[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.ReturnValue)] |
You can apply the XmlRootAttribute to a class, struct, enumeration, or interface. You can also apply the attribute to the return value of an XML Web service method.
Every XML document must have a single root element that contains all the other elements. The XmlRootAttribute allows you to control how the XmlSerializer generates the root element by setting certain properties. For example, specify the name of the generated XML element by setting the XmlRootAttribute.ElementName property.
For more information about using attributes, see the conceptual topic at MSDN: extendingmetadatausingattributes.
[XmlRootAttribute(Namespace="http://www.cpandl.com", ElementName = "MyClassName", Form = XmlSchemaForm.Qualified, IsNullable = false)] public class MyClass { private string mystring; public string MyStringProperty { get{return mystring;} set{mystring = value;} } }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the XmlRootAttribute class. |
ctor #2 | Overloaded:.ctor(string elementName) Initializes a new instance of the XmlRootAttribute class, and specifies the name of the XML root element. |
DataType | Read-write Gets or sets the XSD data type of the XML root element. |
ElementName | Read-write Gets or sets the name of the XML element that is generated and recognized by the XmlSerializer class's XmlSerializer.Serialize and XmlSerializer.Deserialize methods, respectively. |
IsNullable | Read-write Gets or sets a value indicating whether the XmlSerializer should serialize a member that is set to null into the xsi:nil attribute set to true. |
Namespace | Read-write Gets or sets the namespace for the XML root element. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XmlRootAttribute(); |
using System; using System.IO; using System.Xml.Serialization; // This is the class that is the default root element. public class MyClass { public string Name; } public class Run { public static void Main() { Run test = new Run(); test.SerializeOrder("OverrideAttribute.xml"); } public void SerializeOrder(string filename) { // Create an XmlSerializer instance using the method below. XmlSerializer xSer = CreateOverrider(); // Create the object, and set its Name property. MyClass myClass = new MyClass(); myClass.Name = "New Class Name"; // Serialize the class, and close the TextWriter. TextWriter writer = new StreamWriter(filename); xSer.Serialize(writer, myClass); writer.Close(); } // Return an XmlSerializer to override the root serialization. public XmlSerializer CreateOverrider() { // Create an XmlAttributes to override the default root element. XmlAttributes attrs = new XmlAttributes(); // Create an XmlRootAttribute and set its element name and namespace. XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "OverriddenRootElementName"; xRoot.Namespace = "http://www.microsoft.com"; // Set the XmlRoot property to the XmlRoot object. attrs.XmlRoot = xRoot; XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Add the XmlAttributes object to the XmlAttributeOverrides object. */ xOver.Add(typeof(MyClass), attrs); // Create the Serializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(MyClass), xOver); return xSer; } }
public XmlRootAttribute( |
elementName
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 overloaded constructer //and set its namespace. XmlRootAttribute myXmlRootAttribute = new XmlRootAttribute("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 string DataType {get; set;}
|
For the XSD base64Binary and hexBinary data types, use an array of Byte structures, and apply a XmlRootAttribute with the XmlRootAttribute.DataType property set to "base64Binary" or "hexBinary", as appropriate. For the XSD time and date data types, use the DateTime type and apply the XmlRootAttribute with the XmlRootAttribute.DataType set to "date" or "time".
For every XSD type that is mapped to a string, apply the XmlRootAttribute with its XmlRootAttribute.DataType property set to the XSD type. However, this will not change the serialization format, only the schema for the member.
For more information about XML data types, see the World Wide Web Consortium (www.w3.org ) document named "XML Schema Part 2: Datatypes".
XSD data type | .NET data type |
---|---|
anyURI | String |
base64Binary | Array of Byte objects |
boolean | Boolean |
byte | SByte |
date | DateTime |
dateTime | DateTime |
decimal | Decimal |
double | Double |
ENTITY | String |
ENTITIES | String |
float | Single |
gDay | String |
gMonth | String |
gMonthDay | String |
gYear | String |
gYearMonth | String |
hexBinary | Array of Byte objects |
ID | String |
IDREF | String |
IDREFS | String |
int | Int32 |
integer | String |
language | String |
long | Int64 |
Name | String |
NCName | String |
negativeInteger | String |
NMTOKEN | String |
NMTOKENS | String |
normalizedString | String |
nonNegativeInteger | String |
nonPositiveInteger | String |
NOTATION | String |
positiveInteger | String |
QName | XmlQualifiedName |
recurringDate | String |
duration | String |
string | String |
short | Int16 |
time | DateTime |
token | String |
unsignedByte | Byte |
unsignedInt | UInt32 |
unsignedLong | UInt64 |
unsignedShort | UInt16 |
public string ElementName {get; set;}
|
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 bool IsNullable {get; set;}
|
If the XmlRootAttribute.IsNullable property is set to true, the xsi:nil attribute is generated as shown in the following XML:
<?xml version="1.0" encoding="utf-8"?> <Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />
If the XmlRootAttribute.IsNullable property is false, an empty element is created as shown below:
<?xml version="1.0" encoding="utf-8"?> <Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
using System; using System.IO; using System.Xml.Serialization; using System.Xml; // Apply the XmlRootAttribute and set the IsNullable property to false. [XmlRoot(IsNullable = false)] public class Group { public string Name; } public class Run { public static void Main() { Console.WriteLine("Running"); Run test = new Run(); test.SerializeObject("NullDoc.xml"); } public void SerializeObject(string filename) { XmlSerializer s = new XmlSerializer(typeof(Group)); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create the object to serialize. Group mygroup = null; // Serialize the object, and close the TextWriter. s.Serialize(writer, mygroup); writer.Close(); } }
public string Namespace {get; set;}
|
To create prefixed namespaces in the XML document, create an XmlSerializerNamespaces object that contains all the prefix-namespace pairs. The namespace you set for each XmlArrayAttribute must be contained in the XmlSerializerNamespaces object. When the XmlSerializer generates the document, it correctly prefixes the element name for each array item.
[XmlRoot(Namespace = "http://www.cpandl.com")] public class Group { // Insert the members of the Group class. }
public virtual object TypeId {get;}
|
~XmlRootAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |