Export (0) Print
Expand All

XmlAttributeAttribute.AttributeName Property

Gets or sets the name of the XML attribute.

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

public string AttributeName { get; set; }
/** @property */
public String get_AttributeName ()

/** @property */
public void set_AttributeName (String value)

public function get AttributeName () : String

public function set AttributeName (value : String)

Property Value

The name of the XML attribute. The default is the member name.

Use the AttributeName property to specify an XML attribute name when the default value cannot be used. For example, if the XML attribute name is invalid as a member identifier, you can use a valid name for the identifier while setting the AttributeName to an invalid name.

The following example sets the AttributeName property of an XmlAttributeAttribute.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group
{
   // Change the XML attribute name.
   [XmlAttribute(AttributeName = "MgrName")]
   public string Name;
   /* Use the AttributeName to collect all the XML attributes
   in the XML-document instance. */
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      /* To use the AttributeName to collect all the
      XML attributes. Call SerializeObject to generate 
      an XML document and alter the document by adding
      new XML attributes to it. Then comment out the SerializeObject
      method, and call DeserializeObject. */
      test.SerializeObject("MyAtts.xml");
      test.DeserializeObject("MyAtts.xml");
}
public void SerializeObject(string filename)
{
   Console.WriteLine("Serializing");
   // Create an instance of the XmlSerializer class.
   XmlSerializer mySerializer =  new XmlSerializer(typeof(Group));
   // 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.Name = "Wallace";
   // Serialize the class, and close the TextWriter.
   mySerializer.Serialize(writer, myGroup);
   writer.Close();
}

   public void DeserializeObject(string filename)
   {
      Console.WriteLine("Deserializing");
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group)
      mySerializer.Deserialize(fs);
      Console.WriteLine(myGroup.Name);
   }
}
   

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

public class Group
{
    // Change the XML attribute name.
    /** @attribute XmlAttribute(AttributeName = "MgrName")
     */
    public String name;
    /* Use the AttributeName to collect all the XML attributes
       in the XML-document instance.
     */
} //Group

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        /* To use the AttributeName to collect all the
           XML attributes. Call SerializeObject to generate 
           an XML document and alter the document by adding
           new XML attributes to it. Then comment out the SerializeObject
           method, and call DeserializeObject.
         */
        test.SerializeObject("MyAtts.xml");
        test.DeserializeObject("MyAtts.xml");
    } //main

    public void SerializeObject(String fileName)
    {
        Console.WriteLine("Serializing");
        // Create an instance of the XmlSerializer class.
        XmlSerializer mySerializer = new XmlSerializer(Group.class.ToType());
        // 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.name = "Wallace";
        // Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        Console.WriteLine("Deserializing");
        XmlSerializer mySerializer = new XmlSerializer(Group.class.ToType());
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);
        Console.WriteLine(myGroup.name);
    } //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, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft