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.

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

public XmlRootAttribute XmlRoot { get; set; }
/** @property */
public XmlRootAttribute get_XmlRoot ()

/** @property */
public void set_XmlRoot (XmlRootAttribute value)

public function get XmlRoot () : XmlRootAttribute

public function set XmlRoot (value : XmlRootAttribute)

Not applicable.

Property Value

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;
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();
   }
}


import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

// This is the class that will be serialized.
public class Group
{
    public String groupName;
    /** @attribute XmlAttribute()
     */
    public int groupCode;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializeObject("OverrideRoot.xml");
    } //main

    // 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.set_Namespace("http://www.cpandl.com");
        xRoot.set_ElementName("NewGroup");
        attrs.set_XmlRoot(xRoot);

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

        // Get the XmlAttributes object, based on the type.
        XmlAttributes tempAttrs;
        tempAttrs = xOver.get_Item(Group.class.ToType());

        // Print the Namespace and ElementName of the root.
        Console.WriteLine(tempAttrs.get_XmlRoot().get_Namespace());
        Console.WriteLine(tempAttrs.get_XmlRoot().get_ElementName());

        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider

    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();
    } //SerializeObject
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft