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.

[Visual Basic]
Public Property ElementName As String
[C#]
public string ElementName {get; set;}
[C++]
public: __property String* get_ElementName();
public: __property void set_ElementName(String*);
[JScript]
public function get ElementName() : String;
public function set ElementName(String);

Property Value

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.

Remarks

Specify an ElementName if you want the name of the generated XML element to differ from the class name.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub SerializeOrder(filename As String)
   ' Create an XmlSerializer instance using the method below.
   Dim myXmlSerializer As XmlSerializer = CreateOverrider()

   ' Create the object, and set its Name property.
   Dim myStudent As New Student()
   myStudent.Name = "Student class1"

   ' Serialize the class, and close the TextWriter.
   Dim writer = New StreamWriter(filename)
   myXmlSerializer.Serialize(writer, myStudent)
   writer.Close()
End Sub

' Return an XmlSerializer to override the root serialization.
Public Function CreateOverrider() As XmlSerializer
   ' Create an XmlAttributes to override the default root element.
   Dim myXmlAttributes As New XmlAttributes()

   ' Create an XmlRootAttribute and set its element name and namespace.
   Dim myXmlRootAttribute As New XmlRootAttribute()
   myXmlRootAttribute.ElementName = "OverriddenRootElementName"
   myXmlRootAttribute.Namespace = "http://www.microsoft.com"

   ' Set the XmlRoot property to the XmlRoot object.
   myXmlAttributes.XmlRoot = myXmlRootAttribute
   Dim myXmlAttributeOverrides As New XmlAttributeOverrides()

   ' Add the XmlAttributes object to the XmlAttributeOverrides object.
   myXmlAttributeOverrides.Add(GetType(Student), myXmlAttributes)

   ' Create the Serializer, and return it.
   Dim myXmlSerializer As New XmlSerializer(GetType(Student), myXmlAttributeOverrides)
   Return myXmlSerializer
End Function

[C#] 
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;
}

[C++] 
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;
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlRootAttribute Class | XmlRootAttribute Members | System.Xml.Serialization Namespace

Show:
© 2014 Microsoft