Auf Englisch lesen

Freigeben über


XmlAttributeAttribute Konstruktoren

Definition

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

Überlädt

XmlAttributeAttribute()

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

XmlAttributeAttribute(String)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse und gibt den Namen des generierten XML-Attributs an.

XmlAttributeAttribute(Type)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

XmlAttributeAttribute(String, Type)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

XmlAttributeAttribute()

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

C#
public XmlAttributeAttribute ();

Beispiele

C#
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Student
{
   public string StudentName;
   public int StudentNumber;
}
 
public class Book
{
   public string BookName;
   public int BookNumber;
}
public class XMLAttributeAttribute_ctr1
{
   public static void Main()
   {
      XMLAttributeAttribute_ctr1 myAttribute = 
                                 new XMLAttributeAttribute_ctr1();
      myAttribute.SerializeObject("Student.xml","Book.xml");
   }
 
   public void SerializeObject(string studentFilename,string bookFilename)
   {
      XmlSerializer mySerializer;
      TextWriter writer;

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlAttributeOverrides = 
                                                     new XmlAttributeOverrides();
      XmlAttributes myXmlAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlAttributeAttribute = 
                                                new XmlAttributeAttribute();
      myXmlAttributeAttribute.AttributeName="Name";
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(typeof(Student), "StudentName", 
                                                      myXmlAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Student), 
                                                myXmlAttributeOverrides);
      
      writer = new StreamWriter(studentFilename);

      // Create an instance of the class that will be serialized.
      Student myStudent = new Student();

      // Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName= "James";
      myStudent.StudentNumber=1;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent);
      writer.Close();

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlBookAttributeOverrides = 
                                                new XmlAttributeOverrides();
      XmlAttributes myXmlBookAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlBookAttributeAttribute = 
                                           new XmlAttributeAttribute("Name");
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(typeof(Book), "BookName", 
                                             myXmlBookAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Book), 
                                       myXmlBookAttributeOverrides);
      
      writer = new StreamWriter(bookFilename);

      // Create an instance of the class that will be serialized.
      Book myBook = new Book();

      // Set the Name property, which will be generated as an XML attribute. 
      myBook.BookName= ".NET";
      myBook.BookNumber=10;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook);
      writer.Close();
   }
}

Gilt für

.NET 7 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1
UWP 10.0

XmlAttributeAttribute(String)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse und gibt den Namen des generierten XML-Attributs an.

C#
public XmlAttributeAttribute (string attributeName);
C#
public XmlAttributeAttribute (string? attributeName);

Parameter

attributeName
String

Der Name des von XmlSerializer generierten XML-Attributs.

Beispiele

C#
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Student
{
   public string StudentName;
   public int StudentNumber;
}
 
public class Book
{
   public string BookName;
   public int BookNumber;
}
public class XMLAttributeAttribute_ctr1
{
   public static void Main()
   {
      XMLAttributeAttribute_ctr1 myAttribute = 
                                 new XMLAttributeAttribute_ctr1();
      myAttribute.SerializeObject("Student.xml","Book.xml");
   }
 
   public void SerializeObject(string studentFilename,string bookFilename)
   {
      XmlSerializer mySerializer;
      TextWriter writer;

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlAttributeOverrides = 
                                                     new XmlAttributeOverrides();
      XmlAttributes myXmlAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlAttributeAttribute = 
                                                new XmlAttributeAttribute();
      myXmlAttributeAttribute.AttributeName="Name";
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(typeof(Student), "StudentName", 
                                                      myXmlAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Student), 
                                                myXmlAttributeOverrides);
      
      writer = new StreamWriter(studentFilename);

      // Create an instance of the class that will be serialized.
      Student myStudent = new Student();

      // Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName= "James";
      myStudent.StudentNumber=1;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent);
      writer.Close();

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlBookAttributeOverrides = 
                                                new XmlAttributeOverrides();
      XmlAttributes myXmlBookAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlBookAttributeAttribute = 
                                           new XmlAttributeAttribute("Name");
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(typeof(Book), "BookName", 
                                             myXmlBookAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Book), 
                                       myXmlBookAttributeOverrides);
      
      writer = new StreamWriter(bookFilename);

      // Create an instance of the class that will be serialized.
      Book myBook = new Book();

      // Set the Name property, which will be generated as an XML attribute. 
      myBook.BookName= ".NET";
      myBook.BookNumber=10;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook);
      writer.Close();
   }
}

Gilt für

.NET 7 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1
UWP 10.0

XmlAttributeAttribute(Type)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

C#
public XmlAttributeAttribute (Type type);
C#
public XmlAttributeAttribute (Type? type);

Parameter

type
Type

Der zum Speichern des Attributs verwendete Type.

Gilt für

.NET 7 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1
UWP 10.0

XmlAttributeAttribute(String, Type)

Initialisiert eine neue Instanz der XmlAttributeAttribute-Klasse.

C#
public XmlAttributeAttribute (string attributeName, Type type);
C#
public XmlAttributeAttribute (string? attributeName, Type? type);

Parameter

attributeName
String

Der Name des generierten XML-Attributs.

type
Type

Der zum Speichern des Attributs verwendete Type.

Gilt für

.NET 7 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1
UWP 10.0