XmlRootAttribute Constructor (String^)

 

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
)

Parameters

elementName
Type: System::String^

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 = gcnew Student;
      myStudent->Name = "Student class1";

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

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

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

      // Set the XmlRoot property to the XmlRoot object.
      myXmlAttributes->XmlRoot = myXmlRootAttribute;
      XmlAttributeOverrides^ myXmlAttributeOverrides =
         gcnew XmlAttributeOverrides;

      // Add the XmlAttributes object to the XmlAttributeOverrides object
      myXmlAttributeOverrides->Add( Student::typeid, myXmlAttributes );

      // Create the Serializer, and return it.
      XmlSerializer^ myXmlSerializer = gcnew XmlSerializer(
         Student::typeid, myXmlAttributeOverrides );
      return myXmlSerializer;
   }

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: