Export (0) Print
Expand All

XmlAttributes.XmlAttribute Property

Gets or sets an object that specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute.

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

public XmlAttributeAttribute XmlAttribute { get; set; }
/** @property */
public XmlAttributeAttribute get_XmlAttribute ()

/** @property */
public void set_XmlAttribute (XmlAttributeAttribute value)

public function get XmlAttribute () : XmlAttributeAttribute

public function set XmlAttribute (value : XmlAttributeAttribute)

Property Value

An XmlAttributeAttribute that controls the serialization of a public field or read/write property as an XML attribute.

By default, if no attribute is applied to a public field or public read/write property, it is serialized as an XML element. You can also instruct the XmlSerializer to generate an XML attribute by applying an XmlAttributeAttribute to the field or property.

The XmlAttribute property allows you to override the default serialization, as well as the serialization controlled by applying an XmlAttributeAttribute to the member. To do this, create an XmlAttributeAttribute and set its properties (such as AttributeName). Assign the new object to the XmlAttribute property of an XmlAttributes object. Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property, and the name of the field or property. Lastly, create an XmlSerializer using the XmlAttributeOverrides object and call the Serialize or Deserialize method.

The following example serializes a class named Group that contains a property named GroupName; the GroupName property is serialized as an XML attribute. The example creates an XmlAttributeOverrides and an XmlAttributes object to override the default serialization of the field. The example then creates an XmlAttributeAttribute to specifically override the property, and the object is set to the XmlAttribute property The XmlAttributes object is added to the XmlAttributeOverrides object with the name of the overridden member specified. Finally, an XmlSerializer is constructed and returned using the XmlAttributeOverrides object.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   // This is the attribute that will be overridden.
   [XmlAttribute]
   public string GroupName;
   public int GroupNumber;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideAttribute.xml");
      test.DeserializeObject("OverrideAttribute.xml");
   }
   // Return an XmlSerializer used for overriding. 
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes xAttrs = new XmlAttributes();

      /* Create an overriding XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute xAttribute = new XmlAttributeAttribute("SplinterName");
      xAttrs.XmlAttribute = xAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      xOver.Add(typeof(Group), "GroupName", xAttrs);

      // Create the XmlSerializer and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }
 
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      /* Set the Name property, which will be generated 
      as an XML attribute. */
      myGroup.GroupName = ".NET";
      myGroup.GroupNumber = 1;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      mySerializer.Deserialize(fs);
      
      Console.WriteLine(myGroup.GroupName);
      Console.WriteLine(myGroup.GroupNumber);
   }
}
   

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

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

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideAttribute.xml");
        test.DeserializeObject("OverrideAttribute.xml");
    } //main

    // Return an XmlSerializer used for overriding. 
    public XmlSerializer CreateOverrider()
    {
        // Create the XmlAttributeOverrides and XmlAttributes objects.
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        XmlAttributes xAttrs = new XmlAttributes();

        /* Create an overriding XmlAttributeAttribute set it to 
           the XmlAttribute property of the XmlAttributes object.*/
        XmlAttributeAttribute xAttribute =
            new XmlAttributeAttribute("SplinterName");
        xAttrs.set_XmlAttribute(xAttribute);

        // Add to the XmlAttributeOverrides. Specify the member name.
        xOver.Add(Group.class.ToType(), "groupName", xAttrs);

        // Create the XmlSerializer and return it.
        return new XmlSerializer(Group.class.ToType(), xOver);
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create an instance of the XmlSerializer class.
        XmlSerializer mySerializer = CreateOverrider();

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);

        // Create an instance of the class that will be serialized.
        Group myGroup = new Group();

        /* Set the Name property, which will be generated 
           as an XML attribute. */
        myGroup.groupName = ".NET";
        myGroup.groupNumber = 1;

        // Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlSerializer mySerializer = CreateOverrider();
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);

        Console.WriteLine(myGroup.groupName);
        Console.WriteLine(myGroup.groupNumber);
    } //DeserializeObject
} //Run

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft