XmlAnyElementAttribute 類別

定義

指定成員 (傳回 XmlElementXmlNode 物件陣列的欄位) 包含物件,該物件表示在序列化或還原序列化物件中沒有對應成員的任何 XML 項目。

public ref class XmlAnyElementAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlAnyElementAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlAnyElementAttribute = class
    inherit Attribute
Public Class XmlAnyElementAttribute
Inherits Attribute
繼承
XmlAnyElementAttribute
屬性

範例

下列範例會將 XmlAnyElementAttribute 套用至名為 AllElements 的欄位,以傳回 物件的陣列 XmlElement

public ref class XClass
{
public:

   /* Apply the XmlAnyElementAttribute to a field returning an array
      of XmlElement objects. */

   [XmlAnyElement]
   array<XmlElement^>^AllElements;
};

public ref class Test
{
public:
   void DeserializeObject( String^ filename )
   {
      // Create an XmlSerializer.
      XmlSerializer^ mySerializer = gcnew XmlSerializer( XClass::typeid );

      // To read a file, a FileStream is needed.
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );

      // Deserialize the class.
      XClass^ x = dynamic_cast<XClass^>(mySerializer->Deserialize( fs ));

      // Read the element names and values.
      System::Collections::IEnumerator^ myEnum = x->AllElements->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         XmlElement^ xel = safe_cast<XmlElement^>(myEnum->Current);
         Console::WriteLine( "{0}: {1}", xel->LocalName, xel->Value );
      }
   }
};

int main()
{
   Test^ t = gcnew Test;
   t->DeserializeObject( "XFile.xml" );
}
public class XClass
{
   /* Apply the XmlAnyElementAttribute to a field returning an array
   of XmlElement objects. */
   [XmlAnyElement]
   public XmlElement[] AllElements;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.DeserializeObject("XFile.xml");
   }

   private void DeserializeObject(string filename)
   {
      // Create an XmlSerializer.
      XmlSerializer mySerializer = new XmlSerializer(typeof(XClass));

      // To read a file, a FileStream is needed.
      FileStream fs = new FileStream(filename, FileMode.Open);

      // Deserialize the class.
      XClass x = (XClass) mySerializer.Deserialize(fs);

      // Read the element names and values.
      foreach(XmlElement xel in x.AllElements)
         Console.WriteLine(xel.LocalName + ": " + xel.Value);
   }
}
Public Class XClass
    ' Apply the XmlAnyElementAttribute to a field returning an array
    ' of XmlElement objects.
    <XmlAnyElement()> Public AllElements() As XmlElement
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.DeserializeObject("XFile.xml")
    End Sub
    
    
    Private Sub DeserializeObject(filename As String)
        ' Create an XmlSerializer.
        Dim mySerializer As New XmlSerializer(GetType(XClass))
        
        ' To read a file, a FileStream is needed.
        Dim fs As New FileStream(filename, FileMode.Open)
        
        ' Deserialize the class.
        Dim x As XClass = CType(mySerializer.Deserialize(fs), XClass)
        
        ' Read the element names and values.
        Dim xel As XmlElement
        For Each xel In  x.AllElements
            Console.WriteLine((xel.LocalName & ": " & xel.Value))
        Next xel
    End Sub
End Class

備註

提示

在可攜式類別庫中工作時,例如 Silverlight、Windows Phone 或 Windows 市集 App 專案,而且您使用的是 .NET Framework 4.0.3 和更新版本,請使用 XElementXNode 取代 XmlElementXmlNode

使用 包含 XmlAnyElementAttribute 任意資料 (做為 XML 元素,) 可以當做 XML 檔的一部分傳送,例如當做檔的一部分傳送的中繼資料。

XmlAnyElementAttribute 套用至傳回 或 XmlNode 物件陣列的 XmlElement 欄位。 視物件正在序列化或還原序列化而定,這類欄位可以使用兩種方式。 序列化時,即使物件在序列化的物件中沒有對應的成員 (或) 成員,物件還是會產生為 XML 專案或節點。 如果您在套用屬性時指定 Name 屬性值,則插入陣列中的所有 XmlElementXmlNode 物件都必須具有相同的元素名稱和預設命名空間,否則會擲回例外狀況。 如果您設定 Namespace 屬性值,也必須設定 Name 屬性,而且 XmlElementXmlNode 物件也必須具有相同的名稱和命名空間值。 如果未指定任何 Name 值, XmlElementXmlNode 物件可以有任何專案名稱。

當您呼叫 Deserialize 類別的 XmlSerializer 方法時,在陣列中會收集物件中沒有對應成員的所有專案。 還原序列化之後,逐一查看專案的集合 XmlElement 來處理資料。 如果您指定 Name 值,陣列只包含具有該名稱的 XML 元素。 如果您未指定 Name 值,陣列會包含類別中沒有對應成員的所有專案。 如果類別包含套用屬性的多個欄位,請使用 Name 、 或 NameNamespace 屬性來區分陣列的內容。 如果這類類別 (多個欄位) 也包含一個欄位,其中一個欄位沒有 (設定的屬性值, NameNamespace 還原序列化期間) ,則此陣列包含尚未包含在其他陣列中的任何未知 XML 元素。 如果類別包含一個以上的欄位,但未設定不同的 NameNameNamespace 值,則還原序列化期間的行為不會指定。

您也可以將 XmlAnyElementAttribute 套用至傳回單 XmlElement 一物件的欄位。 如果您這樣做,您必須使用 類別的屬性 XmlElement 和方法,以遞迴方式逐一查看未知的專案。

您可以將 的 XmlAnyElementAttribute 多個實例套用至類別成員,但每個實例都必須有相異 Name 的屬性值。 或者,如果為每個實例設定相同的 Name 屬性,則必須為每個實例設定不同的 Namespace 屬性值。

UnknownNode如果您將 套用 XmlAnyElementAttribute 至類別的成員,則不會發生 的 XmlSerializerUnknownAttribute 事件。

注意

您可以在程式碼中使用這個字, XmlAnyElement 而不是較長 XmlAnyElementAttribute 的 。

如需使用屬性的詳細資訊,請參閱 屬性

建構函式

XmlAnyElementAttribute()

初始化 XmlAnyElementAttribute 類別的新執行個體。

XmlAnyElementAttribute(String)

初始化 XmlAnyElementAttribute 類別的新執行個體,指定 XML 文件中產生的 XML 項目名稱。

XmlAnyElementAttribute(String, String)

初始化 XmlAnyElementAttribute 類別的新執行個體,指定 XML 文件中產生的 XML 項目名稱及其 XML 命名空間。

屬性

Name

取得或設定 XML 項目名稱。

Namespace

取得或設定在 XML 文件中產生的 XML 命名空間。

Order

取得或設定項目序列化或還原序列化的明確順序。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱