This documentation is archived and is not being maintained.

XmlAttributeAttribute Constructor

Initializes a new instance of the XmlAttributeAttribute class.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

public XmlAttributeAttribute()

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();

   }
}
#using <mscorlib.dll>
#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public __gc class Student
{
public:
   String*	 StudentName;
   int		StudentNumber;
};

public __gc class Book
{
public:
   String*		BookName;
   int			BookNumber;
};

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 = S"Name";
   myXmlAttributes->XmlAttribute = myXmlAttributeAttribute;


   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlAttributeOverrides->Add( __typeof(Student), S"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 = S"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(S"Name");
   myXmlBookAttributes->XmlAttribute = myXmlBookAttributeAttribute;


   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlBookAttributeOverrides->Add(__typeof(Book), S"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= S".NET";
   myBook->BookNumber=10;
   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize(writer, myBook);
   writer->Close();
}

int main()
{
   SerializeObject("Student.xml","Book.xml");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: