XmlAnyElementAttribute XmlAnyElementAttribute XmlAnyElementAttribute XmlAnyElementAttribute Class

定義

指定成員 (傳回 XmlElementXmlNode 物件陣列的欄位) 包含物件,該物件表示在序列化或還原序列化物件中沒有對應成員的任何 XML 項目。Specifies that the member (a field that returns an array of XmlElement or XmlNode objects) contains objects that represent any XML element that has no corresponding member in the object being serialized or deserialized.

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

範例

下列範例會套用XmlAnyElementAttribute名為欄位AllElements所傳回的陣列XmlElement物件。The following example applies the XmlAnyElementAttribute to a field named AllElements that returns an array of XmlElement objects.

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 'XClass


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.DeserializeObject("XFile.xml")
    End Sub 'Main
    
    
    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 'DeserializeObject
End Class 'Test 

備註

提示

當使用可攜式類別庫,例如 Silverlight、 Windows Phone 或 Windows 市集應用程式專案,並使用.NET Framework 4.0.3 和以上所述,使用XElement或是XNode代替XmlElementXmlNodeWhen working in a portable class library, such as in Silverlight, Windows Phone or Windows Store App project, and you are using the .NET Framework 4.0.3 and above, use XElement or XNode in place of XmlElement and XmlNode.

使用XmlAnyElementAttribute包含 (做為 XML 項目) 可以做為 XML 文件,例如做為文件的一部分傳送的中繼資料的一部分傳送的任意資料。Use the XmlAnyElementAttribute to contain arbitrary data (as XML elements) that can be sent as part of an XML document, such as metadata sent as part of the document.

適用於XmlAnyElementAttribute的欄位,傳回的陣列XmlElementXmlNode物件。Apply the XmlAnyElementAttribute to a field that returns an array of XmlElement or XmlNode objects. 這類欄位可以用兩種方式,根據物件是否正在序列化或還原序列化。Such a field can be used in two ways, depending on whether an object is being serialized or deserialized. 當序列化時,會產生物件為 XML 元素或節點,即使它們有沒有對應成員 (或成員) 物件中要序列化。When serialized, the object is generated as XML elements or nodes, even though they have no corresponding member (or members) in the object being serialized. 如果您指定Name屬性值時套用此屬性中,所有XmlElementXmlNode插入陣列的物件必須具有相同項目名稱和預設的命名空間,或擲回例外狀況。If you specify a Name property value when applying the attribute, all XmlElement or XmlNode objects inserted into the array must have the same element name and default namespace, or an exception is thrown. 如果您設定Namespace屬性值,您必須設定Name屬性,而XmlElementXmlNode物件也必須有相同的名稱和命名空間值。If you set the Namespace property value, you must set the Name property as well, and the XmlElement or XmlNode objects must also have the same name and namespace values. 如果沒有Name指定值,則XmlElementXmlNode物件可以有任何項目名稱。If no Name value is specified, the XmlElement or XmlNode objects can have any element name.

當您呼叫Deserialize方法的XmlSerializer類別、 正在還原序列化之物件中沒有對應成員的所有項目會收集陣列中。When you call the Deserialize method of the XmlSerializer class, all elements that do not have a corresponding member in the object being deserialized are collected in the array. 之後還原序列化時,逐一查看集合的XmlElement處理資料的項目。After deserialization, iterate through the collection of XmlElement items to process the data. 如果您指定Name陣列的值,包含僅具有該名稱的 XML 項目。If you specify a Name value, the array contains only XML elements with that name. 如果您未指定Name值,陣列會包含在類別中有沒有對應成員的所有項目。If you do not specify a Name value, the array contains all elements that have no corresponding member in the class. 如果類別包含要套用屬性的多個欄位,使用Name,或NameNamespace屬性區分陣列的內容。If a class contains more than one field to which the attribute is applied, use the Name, or Name and Namespace properties to differentiate between the contents of the arrays. 如果這種類別 (使用多個欄位) 也會包含一個已設定任何區分用的屬性值的欄位 (亦即NameNamespace) 還原序列化期間,這個陣列包含已不包含任何未知的 XML 項目在 其他陣列。If such a class (with multiple fields) also contains one field that has no differentiating property values set (in other words, Name and Namespace) during deserialization, this array contains any unknown XML elements that are not already contained in the other arrays. 如果類別包含一個以上的欄位沒有區別Name,或NameNamespace設定值,在還原序列化期間的行為是未指定。If a class contains more than one field that does not have a differentiating Name, or Name and Namespace value set, the behavior during deserialization is unspecified.

您也可以套用XmlAnyElementAttribute的欄位,傳回單一XmlElement物件。You can also apply the XmlAnyElementAttribute to a field that returns a single XmlElement object. 如果您這樣做,您必須使用的屬性和方法XmlElement類別以遞迴方式逐一查看未知的項目。If you do so, you must use the properties and methods of the XmlElement class to recursively iterate through the unknown elements.

您可以套用多個執行個體XmlAnyElementAttribute的類別成員,但每個執行個體都必須相異Name屬性值。You can apply multiple instances of the XmlAnyElementAttribute to a class member, but each instance must have a distinct Name property value. 或者,如果相同Name屬性會設為每個執行個體,相異Namespace屬性值必須為每個執行個體。Or, if the same Name property is set for each instance, a distinct Namespace property value must be set for each instance.

UnknownNodeUnknownAttribute事件XmlSerializer如果您將不會發生XmlAnyElementAttribute類別的成員。The UnknownNode and UnknownAttribute events of the XmlSerializer do not occur if you apply the XmlAnyElementAttribute to a member of a class.

注意

您可以使用 wordXmlAnyElement代替較長的程式碼中XmlAnyElementAttributeYou can use the word XmlAnyElement in your code instead of the longer XmlAnyElementAttribute.

如需使用屬性的詳細資訊,請參閱屬性For more information about using attributes, see Attributes.

建構函式

XmlAnyElementAttribute() XmlAnyElementAttribute() XmlAnyElementAttribute() XmlAnyElementAttribute()

初始化 XmlAnyElementAttribute 類別的新執行個體。Initializes a new instance of the XmlAnyElementAttribute class.

XmlAnyElementAttribute(String) XmlAnyElementAttribute(String) XmlAnyElementAttribute(String) XmlAnyElementAttribute(String)

初始化 XmlAnyElementAttribute 類別的新執行個體,指定 XML 文件中產生的 XML 項目名稱。Initializes a new instance of the XmlAnyElementAttribute class and specifies the XML element name generated in the XML document.

XmlAnyElementAttribute(String, String) XmlAnyElementAttribute(String, String) XmlAnyElementAttribute(String, String) XmlAnyElementAttribute(String, String)

初始化 XmlAnyElementAttribute 類別的新執行個體,指定 XML 文件中產生的 XML 項目名稱及其 XML 命名空間。Initializes a new instance of the XmlAnyElementAttribute class and specifies the XML element name generated in the XML document and its XML namespace.

屬性

Name Name Name Name

取得或設定 XML 項目名稱。Gets or sets the XML element name.

Namespace Namespace Namespace Namespace

取得或設定在 XML 文件中產生的 XML 命名空間。Gets or sets the XML namespace generated in the XML document.

Order Order Order Order

取得或設定項目序列化或還原序列化的明確順序。Gets or sets the explicit order in which the elements are serialized or deserialized.

TypeId TypeId TypeId TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

適用於

另請參閱