XmlRootAttribute Constructor (String)
.NET Framework 4
Initializes a new instance of the XmlRootAttribute class and specifies the name of the XML root element.
Assembly: System.Xml (in System.Xml.dll)
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; }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.