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 function get XmlRoot () : XmlRootAttribute 
public function set XmlRoot (value : XmlRootAttribute)

Property Value

Type: System.Xml.Serialization.XmlRootAttribute
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.)

No code example is currently available or this language may not be supported.
#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");
}

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

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft