Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlRootAttribute.ElementName Property

Gets or sets the name of the XML element that is generated and recognized by the XmlSerializer class's Serialize and Deserialize methods, respectively.

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

public string ElementName { get; set; }

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 = 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 and set its element name and namespace.
   XmlRootAttribute myXmlRootAttribute = new XmlRootAttribute();
   myXmlRootAttribute.ElementName = "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 = S"Student class1";

      // Serialize the class, and close the TextWriter.
      TextWriter* writer = new 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 = new XmlAttributes();

      // Create an XmlRootAttribute and set its element name and namespace.
      XmlRootAttribute* myXmlRootAttribute = new XmlRootAttribute();
      myXmlRootAttribute -> ElementName = S"OverriddenRootElementName";
      myXmlRootAttribute -> Namespace = S"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, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft