XmlAnyElementAttribute 类

定义

指定成员(返回 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)]
public class XmlAnyElementAttribute : Attribute
type XmlAnyElementAttribute = class
    inherit Attribute
Public Class XmlAnyElementAttribute
Inherits Attribute
继承
XmlAnyElementAttribute
属性

示例

下面的示例将 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


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 应用商店应用程序项目)中工作时,如果使用 .NET Framework 4.0.3 和更高版本,请使用 XElementXNode 代替 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.

调用 XmlSerializer 类的 Deserialize 方法时,数组中将收集在要反序列化的对象中没有相应成员的所有元素。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.

如果将 XmlAnyElementAttribute 应用于类的成员,则不会发生 XmlSerializerUnknownNodeUnknownAttribute 事件。The UnknownNode and UnknownAttribute events of the XmlSerializer do not occur if you apply the XmlAnyElementAttribute to a member of a class.

备注

您可以在代码中使用 word XmlAnyElement 而不是更长的 XmlAnyElementAttributeYou can use the word XmlAnyElement in your code instead of the longer XmlAnyElementAttribute.

有关使用特性的详细信息,请参阅特性For more information about using attributes, see Attributes.

构造函数

XmlAnyElementAttribute()

初始化 XmlAnyElementAttribute 类的新实例。Initializes a new instance of the XmlAnyElementAttribute class.

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 类的新实例并指定在 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

获取或设置 XML 元素名。Gets or sets the XML element name.

Namespace

获取或设置在 XML 文档中生成的 XML 命名空间。Gets or sets the XML namespace generated in the XML document.

Order

获取或设置序列化或反序列化元素的显式顺序。Gets or sets the explicit order in which the elements are serialized or deserialized.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅