XmlAttributes::XmlRoot Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets an object that specifies how the XmlSerializer serializes a class as an XML root element.

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

property XmlRootAttribute^ XmlRoot {
	XmlRootAttribute^ get();
	void set(XmlRootAttribute^ value);

Property Value

Type: System.Xml.Serialization::XmlRootAttribute^

An XmlRootAttribute that overrides a class attributed as an XML root element.

The following example creates an XmlAttributeOverrides object, an XmlAttributes object, and an XmlRootAttribute object. The example assigns the XmlRootAttribute to the XmlRoot property of the XmlAttributes object, and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class to the XmlAttributeOverrides object. (In this example, the Type is Group.)

#using <System.dll>
#using <System.Xml.dll>

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

// This is the class that will be serialized.
public ref class Group
   String^ GroupName;

   int GroupCode;

// Return an XmlSerializer for overriding attributes.
XmlSerializer^ CreateOverrider()
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes^ attrs = gcnew XmlAttributes;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlRootAttribute^ xRoot = gcnew XmlRootAttribute;

   // Set a new Namespace and ElementName for the root element.
   xRoot->Namespace = "http://www.cpandl.com";
   xRoot->ElementName = "NewGroup";
   attrs->XmlRoot = xRoot;

   /* Add the XmlAttributes object to the XmlAttributeOverrides. 
      No  member name is needed because the whole class is 
      overridden. */
   xOver->Add( Group::typeid, attrs );

   // Get the XmlAttributes object, based on the type.
   XmlAttributes^ tempAttrs;
   tempAttrs = xOver[ Group::typeid ];

   // Print the Namespace and ElementName of the root.
   Console::WriteLine( tempAttrs->XmlRoot->Namespace );
   Console::WriteLine( tempAttrs->XmlRoot->ElementName );
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
   return xSer;

void SerializeObject( String^ filename )
   // Create the XmlSerializer using the CreateOverrider method.
   XmlSerializer^ xSer = CreateOverrider();

   // Create the object to serialize.
   Group^ myGroup = gcnew Group;
   myGroup->GroupName = ".NET";
   myGroup->GroupCode = 123;

   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Serialize the object and close the TextWriter.
   xSer->Serialize( writer, myGroup );

int main()
   SerializeObject( "OverrideRoot.xml" );

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