次の方法で共有


XmlAttributeAttribute.AttributeName プロパティ

XML 属性の名前を取得または設定します。

Public Property AttributeName As String
[C#]
public string AttributeName {get; set;}
[C++]
public: __property String* get_AttributeName();public: __property void set_AttributeName(String*);
[JScript]
public function get AttributeName() : String;public function set AttributeName(String);

プロパティ値

XML 属性の名前。既定値は、メンバ名です。

解説

既定値が使用できない場合は、 AttributeName プロパティを使用して、XML 属性名を指定します。たとえば、XML 属性名がメンバ識別子としては無効である場合は、 AttributeName を無効な名前に設定する一方で、識別子に対して有効な名前を使用できます。

使用例

[Visual Basic, C#, C++] XmlAttributeAttributeAttributeName プロパティを設定する例を次に示します。

 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


Public Class Group
    ' Change the XML attribute name.
    <XmlAttribute(AttributeName := "MgrName")> Public Name As String
    ' Use the AttributeName to collect all the XML attributes
    ' in the XML-document instance. 
    <XmlAttribute(AttributeName := "*")> Public Attrs() As XmlAttribute
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As 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")
    End Sub
    
    Public Sub SerializeObject(ByVal filename As String)
        Console.WriteLine("Serializing")
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As New XmlSerializer(GetType(Group))
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        ' Create an instance of the class that will be serialized.
        Dim myGroup As 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()
    End Sub
        
    Public Sub DeserializeObject(ByVal filename As String)
        Console.WriteLine("Deserializing")
        Dim mySerializer As New XmlSerializer(GetType(Group))
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        ' The following code prints all the attributes in the
        ' XML document. To collect the attributes, the AttributeName of the
        ' XmlAttributeAttribute must be set to "*".
        Dim xAtt As XmlAttribute
        For Each xAtt In  myGroup.Attrs
            Console.WriteLine(xAtt.Name & ":" & xAtt.Value)
        Next xAtt
    End Sub
End Class


[C#] 
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);
   }
}
   

[C++] 
#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");
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlAttributeAttribute クラス | XmlAttributeAttribute メンバ | System.Xml.Serialization 名前空間 | ElementName