共用方式為


XmlAttributes.XmlAttribute 屬性

定義

取得或設定物件,指定 XmlSerializer 如何將公用欄位或公用讀取/寫入屬性序列化為 XML 屬性。

public:
 property System::Xml::Serialization::XmlAttributeAttribute ^ XmlAttribute { System::Xml::Serialization::XmlAttributeAttribute ^ get(); void set(System::Xml::Serialization::XmlAttributeAttribute ^ value); };
public System.Xml.Serialization.XmlAttributeAttribute XmlAttribute { get; set; }
public System.Xml.Serialization.XmlAttributeAttribute? XmlAttribute { get; set; }
member this.XmlAttribute : System.Xml.Serialization.XmlAttributeAttribute with get, set
Public Property XmlAttribute As XmlAttributeAttribute

屬性值

XmlAttributeAttribute

XmlAttributeAttribute,控制將公用欄位或讀取/寫入屬性 (Property) 序列化為 XML 屬性 (Attribute)。

範例

下列範例會序列化名為 Group 的類別,其中包含名為 GroupName 的屬性;屬性 GroupName 會序列化為 XML 屬性。 此範例會 XmlAttributeOverrides 建立 和 XmlAttributes 物件,以覆寫欄位的預設序列化。 然後,此範例會 XmlAttributeAttribute 建立 以特別覆寫 屬性,並將 物件設定 XmlAttribute 為 屬性 。 XmlAttributes 物件會新增至物件, XmlAttributeOverrides 並指定覆寫成員的名稱。 最後, XmlSerializer 會使用 XmlAttributeOverrides 物件建構並傳回 。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public ref class Group
{
public:

   // This is the attribute that will be overridden.

   [XmlAttributeAttribute]
   String^ GroupName;
   int GroupNumber;
};

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

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

   // Add to the XmlAttributeOverrides. Specify the member name.
   xOver->Add( Group::typeid, "GroupName", xAttrs );

   // Create the XmlSerializer and return it.
   return gcnew XmlSerializer( Group::typeid,xOver );
}

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

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew 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();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myGroup->GroupName );
   Console::WriteLine( myGroup->GroupNumber );
}

int main()
{
   SerializeObject( "OverrideAttribute.xml" );
   DeserializeObject( "OverrideAttribute.xml" );
}
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);
   }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class Group
    ' This is the attribute that will be overridden.
    <XmlAttribute()> Public GroupName As String
    Public GroupNumber As Integer
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideAttribute.xml")
        test.DeserializeObject("OverrideAttribute.xml")
    End Sub
    
    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Create an overriding XmlAttributeAttribute set it to
        ' the XmlAttribute property of the XmlAttributes object.
        Dim xAttribute As New XmlAttributeAttribute("SplinterName")
        xAttrs.XmlAttribute = xAttribute
        
        ' Add to the XmlAttributeOverrides. Specify the member name.
        xOver.Add(GetType(Group), "GroupName", xAttrs)
        
        ' Create the XmlSerializer and return it.
        Return New XmlSerializer(GetType(Group), xOver)
    End Function 'CreateOverrider
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' 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.GroupName = ".NET"
        myGroup.GroupNumber = 1
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)

        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine(myGroup.GroupNumber)
    End Sub
End Class

備註

根據預設,如果未將任何屬性套用至公用欄位或公用讀取/寫入屬性,則會序列化為 XML 元素。 您也可以將 套用 XmlAttributeAttribute 至欄位或屬性,指示 XmlSerializer 產生 XML 屬性。

屬性 XmlAttribute 可讓您覆寫預設序列化,以及套用 XmlAttributeAttribute 至成員所控制的序列化。 若要這樣做,請建立 XmlAttributeAttribute 並設定其屬性 (,例如 AttributeName) 。 將新物件指派給 XmlAttribute 物件的 屬性 XmlAttributes 。 將 XmlAttributes 物件新增至 XmlAttributeOverrides 物件,並指定包含欄位或屬性的物件類型,以及欄位或屬性的名稱。 最後,使用 XmlAttributeOverrides 物件建立 XmlSerializer ,並呼叫 SerializeDeserialize 方法。

適用於