Export (0) Print
Expand All

XmlAttributes.XmlRoot Property

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

[Visual Basic]
Public Property XmlRoot As XmlRootAttribute
[C#]
public XmlRootAttribute XmlRoot {get; set;}
[C++]
public: __property XmlRootAttribute* get_XmlRoot();
public: __property void set_XmlRoot(XmlRootAttribute*);
[JScript]
public function get XmlRoot() : XmlRootAttribute;
public function set XmlRoot(XmlRootAttribute);

Property Value

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

Example

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

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class Group
    Public GroupName As String
    <XmlAttribute()> Public GroupCode As Integer
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("OverrideRoot.xml")
    End Sub
    
    
    ' Return an XmlSerializer for overriding attributes.
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects.
        Dim attrs As New XmlAttributes()
        Dim xOver As New XmlAttributeOverrides()
        
        Dim xRoot As New 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(GetType(Group), attrs)
        
        ' Get the XmlAttributes object, based on the type.
        Dim tempAttrs As XmlAttributes
        tempAttrs = xOver(GetType(Group))
        
        ' Print the Namespace and ElementName of the root.
        Console.WriteLine(tempAttrs.XmlRoot.Namespace)
        Console.WriteLine(tempAttrs.XmlRoot.ElementName)
        
        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create the XmlSerializer using the CreateOverrider method.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object to serialize.
        Dim myGroup As New Group()
        myGroup.GroupName = ".NET"
        myGroup.GroupCode = 123
        
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        
        ' Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class


[C#] 
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("OverrideRoot.xml");
   }

   // Return an XmlSerializer for overriding attributes.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributes and XmlAttributeOverrides objects.
      XmlAttributes attrs = new XmlAttributes();
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
   
      XmlRootAttribute xRoot = new 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(typeof(Group), attrs);

      // Get the XmlAttributes object, based on the type.
      XmlAttributes tempAttrs;
      tempAttrs = xOver[typeof(Group)];

      // Print the Namespace and ElementName of the root.
      Console.WriteLine(tempAttrs.XmlRoot.Namespace);
      Console.WriteLine(tempAttrs.XmlRoot.ElementName);

      XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
      return xSer;
   }

   public void SerializeObject(string filename)
   {
      // Create the XmlSerializer using the CreateOverrider method.
      XmlSerializer xSer = CreateOverrider();

      // Create the object to serialize.
      Group myGroup = new Group();
      myGroup.GroupName = ".NET";
      myGroup.GroupCode = 123;

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

      // Serialize the object and close the TextWriter.
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public __gc class Group
{
public:
   String* GroupName;
   [XmlAttributeAttribute]
   int GroupCode;
};

// Return an XmlSerializer for overriding attributes.
XmlSerializer* CreateOverrider()
{
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes* attrs = new XmlAttributes();
   XmlAttributeOverrides* xOver = new XmlAttributeOverrides();

   XmlRootAttribute* xRoot = new XmlRootAttribute();

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

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

   // Get the XmlAttributes object, based on the type.
   XmlAttributes* tempAttrs;
   tempAttrs = xOver->Item[__typeof(Group)];

   // Print the Namespace and ElementName of the root.
   Console::WriteLine(tempAttrs->XmlRoot->Namespace);
   Console::WriteLine(tempAttrs->XmlRoot->ElementName);

   XmlSerializer* xSer = new XmlSerializer(__typeof(Group), xOver);
   return xSer;
}

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

   // Create the object to serialize.
   Group* myGroup = new Group();
   myGroup->GroupName = S".NET";
   myGroup->GroupCode = 123;

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

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

int main()
{
   SerializeObject(S"OverrideRoot.xml");
}

[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

See Also

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

Show:
© 2014 Microsoft