This documentation is archived and is not being maintained.

XmlRootAttribute.XmlRootAttribute(String) Constructor

Initializes a new instance of the XmlRootAttribute class and specifies the name of the XML root element.

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

public XmlRootAttribute (
	string elementName
)
public XmlRootAttribute (
	String elementName
)
public function XmlRootAttribute (
	elementName : String
)
Not applicable.

Parameters

elementName

The name of the XML root element.

The following example creates an instance of the XmlRootAttribute and uses it to override the serialization of an instance of a class named "Student".

public void SerializeOrder(string filename)
{
   // Create an XmlSerializer instance using the method below.
   XmlSerializer myXmlSerializer = CreateOverrider();

   // Create the object, and set its Name property.
   Student myStudent = new Student();
   myStudent.Name = "Student class1";

   // Serialize the class, and close the TextWriter.
   TextWriter writer = new StreamWriter(filename);
   myXmlSerializer.Serialize(writer, myStudent);
   writer.Close();
}

// Return an XmlSerializer to override the root serialization.
public XmlSerializer CreateOverrider()
{
   // Create an XmlAttributes to override the default root element.
   XmlAttributes myXmlAttributes = new XmlAttributes();

   // Create an XmlRootAttribute overloaded constructer 
   //and set its namespace.
   XmlRootAttribute myXmlRootAttribute = 
                  new XmlRootAttribute("OverriddenRootElementName");
   myXmlRootAttribute.Namespace = "http://www.microsoft.com";

   // Set the XmlRoot property to the XmlRoot object.
   myXmlAttributes.XmlRoot = myXmlRootAttribute;
   XmlAttributeOverrides myXmlAttributeOverrides = 
                                       new XmlAttributeOverrides();
   
   /* Add the XmlAttributes object to the 
   XmlAttributeOverrides object. */
   myXmlAttributeOverrides.Add(typeof(Student), myXmlAttributes);

   // Create the Serializer, and return it.
   XmlSerializer myXmlSerializer = new XmlSerializer
      (typeof(Student), myXmlAttributeOverrides);
   return myXmlSerializer;
}

public void SerializeOrder(String fileName)
{
    // Create an XmlSerializer instance using the method below.
    XmlSerializer myXmlSerializer = CreateOverrider();
    // Create the object, and set its name property.
    Student myStudent = new Student();
    myStudent.name = "Student class1";
    // Serialize the class, and close the TextWriter.
    TextWriter writer = new StreamWriter(fileName);
    myXmlSerializer.Serialize(writer, myStudent);
    writer.Close();
} //SerializeOrder

// Return an XmlSerializer to override the root serialization.
public XmlSerializer CreateOverrider()
{
    // Create an XmlAttributes to override the default root element.
    XmlAttributes myXmlAttributes = new XmlAttributes();
    // Create an XmlRootAttribute overloaded constructer 
    //and set its namespace.
    XmlRootAttribute myXmlRootAttribute = new XmlRootAttribute(
        "OverriddenRootElementName");
    myXmlRootAttribute.set_Namespace("http://www.microsoft.com");
    // Set the XmlRoot property to the XmlRoot object.
    myXmlAttributes.set_XmlRoot(myXmlRootAttribute);
    XmlAttributeOverrides myXmlAttributeOverrides =
        new XmlAttributeOverrides();

    /* Add the XmlAttributes object to the 
       XmlAttributeOverrides object.
     */
    myXmlAttributeOverrides.Add(Student.class.ToType(), myXmlAttributes);
    // Create the Serializer, and return it.
    XmlSerializer myXmlSerializer = new XmlSerializer(
        Student.class.ToType(), myXmlAttributeOverrides);
    return myXmlSerializer;
} //CreateOverrider

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: