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 Value

Type: System.String
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);
   }
}
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public __gc class Group
{
   // Change the XML attribute name.
public:
   [XmlAttributeAttribute(AttributeName = S"MgrName")]
   String* Name;
   /* Use the AttributeName to collect all the XML attributes
   in the XML-document instance. */
};

void SerializeObject(String* filename)
{
   Console::WriteLine(S"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 = S"Wallace";
   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize(writer, myGroup);
   writer->Close();
}

void DeserializeObject(String* filename)
{
   Console::WriteLine(S"Deserializing");
   XmlSerializer* mySerializer =
      new XmlSerializer(__typeof(Group));
   FileStream* fs = new FileStream(filename, FileMode::Open);
   Group* myGroup = dynamic_cast<Group*>(mySerializer->Deserialize(fs));
   Console::WriteLine(myGroup->Name);
}

int main()
{
   /* 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. */
   SerializeObject(S"MyAtts.xml");
   DeserializeObject(S"MyAtts.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, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft