XmlRootAttribute::ElementName Property
.NET Framework (current version)
Gets or sets the name of the XML element that is generated and recognized by the XmlSerializer class's Serialize and Deserialize methods, respectively.
Assembly: System.Xml (in System.Xml.dll)
Property Value
Type: System::String^The name of the XML root element that is generated and recognized in an XML-document instance. The default is the name of the serialized class.
Specify an ElementName if you want the name of the generated XML element to differ from the class name.
The following example creates an instance of the XmlRootAttribute class and sets the ElementName property to a new value. The object is then used to create an XmlAttributeOverrides object used to override the serialization of an object.
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 and set its element name and namespace. XmlRootAttribute^ myXmlRootAttribute = gcnew XmlRootAttribute; myXmlRootAttribute->ElementName = "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
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
Show: