XmlAnyElementAttributes 类

定义

表示 XmlAnyElementAttribute 对象集合。Represents a collection of XmlAnyElementAttribute objects.

public ref class XmlAnyElementAttributes : System::Collections::IList
public ref class XmlAnyElementAttributes : System::Collections::CollectionBase
public class XmlAnyElementAttributes : System.Collections.IList
public class XmlAnyElementAttributes : System.Collections.CollectionBase
type XmlAnyElementAttributes = class
    interface ICollection
    interface IEnumerable
    interface IList
type XmlAnyElementAttributes = class
    inherit CollectionBase
Public Class XmlAnyElementAttributes
Implements IList
Public Class XmlAnyElementAttributes
Inherits CollectionBase
继承
XmlAnyElementAttributes
继承
XmlAnyElementAttributes
实现

示例

下面的示例创建一个新的 XmlAnyElementAttribute ,并将其添加到通过属性访问的对象的集合中 XmlAnyElementsThe following example creates a new XmlAnyElementAttribute and adds it to the collection of objects accessed through the XmlAnyElements property. 然后,将 XmlAttributes 添加到 XmlAttributeOverrides 用于创建的 XmlSerializerThe XmlAttributes is then added to a XmlAttributeOverrides which is used to create an XmlSerializer. XmlSerializer用于对对象进行序列化或反序列化。The XmlSerializer is used to serialize or deserialize an object. 若要查看使用属性的效果 XmlAnyElementAttributes ,请在方法中运行方法来创建名为 UnknownElements.xml 的 XML 文档 SerializeObject MainTo see the effect of using the XmlAnyElementAttributes property, create an XML document named UnknownElements.xml by running the SerializeObject method in the Main method. 编辑生成的文档以包含其他 (未知) 元素。Edit the resulting document to include other (unknown) elements. 注释掉 SerializeObject 方法中的调用 Main ,取消注释对方法的调用 DeserializeObject ,该方法输出任何未知 XML 元素的名称和值。Comment out the SerializeObject call in the Main method, and uncomment the call to the DeserializeObject method, which prints out the name and value of any unknown XML element.

#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
public ref class Group
{
public:
   String^ GroupName;

   [XmlAnyElement]
   array<Object^>^Things;
};

void SerializeObject( String^ filename );
void DeserializeObject( String^ filename );
XmlSerializer^ CreateOverrideSerializer();
int main()
{
   // 1 Run this and create the XML document.
   // 2 Add new elements to the XML document.
   // 3 Comment out the next line, and uncomment
   // the DeserializeObject line to deserialize the
   // XML document and see unknown elements.
   SerializeObject( "UnknownElements.xml" );

   // DeserializeObject(S"UnknownElements.xml");
}

void SerializeObject( String^ filename )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
   TextWriter^ writer = gcnew StreamWriter( filename );
   Group^ g = gcnew Group;
   g->GroupName = "MyGroup";
   ser->Serialize( writer, g );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ ser = CreateOverrideSerializer();

   // A FileStream is needed to read the XML document.
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ g = safe_cast<Group^>(ser->Deserialize( fs ));
   fs->Close();
   Console::WriteLine( g->GroupName );
   Console::WriteLine( g->Things->Length );
   for ( int i = 0; i < g->Things->Length; ++i )
   {
      XmlElement^ xelement = safe_cast<XmlElement^>(g->Things[ i ]);
      Console::WriteLine( "{0}: {1}", xelement->Name, xelement->InnerXml );
   }
}

XmlSerializer^ CreateOverrideSerializer()
{
   XmlAnyElementAttribute^ myAnyElement = gcnew XmlAnyElementAttribute;
   XmlAttributeOverrides^ xOverride = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAtts = gcnew XmlAttributes;
   xAtts->XmlAnyElements->Add( myAnyElement );
   xOverride->Add( Group::typeid, "Things", xAtts );
   return gcnew XmlSerializer( Group::typeid,xOverride );
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   [XmlAnyElement]
   public object[]Things;
}

public class Test{
   static void Main(){
      Test t = new Test();
      // 1 Run this and create the XML document.
      // 2 Add new elements to the XML document.
      // 3 Comment out the new line, and uncomment
      // the DeserializeObject line to deserialize the
      // XML document and see unknown elements.
      t.SerializeObject("UnknownElements.xml");
     
      // t.DeserializeObject("UnknownElements.xml");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof (Group));
      TextWriter writer = new StreamWriter(filename);
      Group g = new Group();
      g.GroupName = "MyGroup";
      ser.Serialize(writer, g);
      writer.Close();
   }

   private void DeserializeObject(string filename){

      XmlSerializer ser = CreateOverrideSerializer();
      // A FileStream is needed to read the XML document.
      FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group)
        ser.Deserialize(fs);
     fs.Close();
     Console.WriteLine(g.GroupName);
     Console.WriteLine(g.Things.Length);
     foreach(XmlElement xelement in g.Things){
     Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
     }
   }

   private XmlSerializer CreateOverrideSerializer(){
      XmlAnyElementAttribute myAnyElement = 
      new XmlAnyElementAttribute();
      XmlAttributeOverrides xOverride = 
      new XmlAttributeOverrides();
      XmlAttributes xAtts = new XmlAttributes();
      xAtts.XmlAnyElements.Add(myAnyElement);
      xOverride.Add(typeof(Group), "Things", xAtts);
      return new XmlSerializer(typeof(Group) , xOverride);
   }
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml

Public Class Group
   Public GroupName As String 
   <XmlAnyElement> _
   Public Things () As object
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
      ' 1 Run this and create the XML document.
      ' 2 Add New elements to the XML document.
      ' 3 Comment out the New line, and uncomment
      ' the DeserializeObject line to deserialize the
      ' XML document and see unknown elements.
     t.SerializeObject("UnknownElements.xml")
     
      't.DeserializeObject("UnknownElements.xml")
   End Sub

   Private Sub SerializeObject(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType (Group))
      Dim writer As TextWriter = New StreamWriter(filename)
      
      Dim g As Group = New Group()
      g.GroupName = "MyGroup"
      ser.Serialize(writer, g)
      writer.Close()
   End Sub

   
   Private Sub DeserializeObject(filename As String)

      Dim ser As XmlSerializer = CreateOverrideSerializer()
      ' A FileStream is needed to read the XML document.
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
     Dim g As Group = CType( _
        ser.Deserialize(fs), Group)
     fs.Close()
     Console.WriteLine(g.GroupName)
     Console.WriteLine(g.Things.Length)
     Dim xelement As XmlELement
     for each xelement in g.Things
        Console.WriteLine(xelement.Name &": " & xelement.InnerXml)
     next
   End Sub
   

   Private Function CreateOverrideSerializer() As XmlSerializer 
      Dim myAnyElement As XmlAnyElementAttribute = _
      New XmlAnyElementAttribute()
      Dim xOverride As XmlAttributeOverrides = _
      New XmlAttributeOverrides()
      Dim xAtts As XmlAttributes = New XmlAttributes()
      xAtts.XmlAnyElements.Add(myAnyElement)
      xOverride.Add(GetType(Group), "Things", xAtts)
      return New XmlSerializer(GetType(Group) , xOverride)
   End Function
End Class

注解

使用 XmlAnyElementAttributes 可以重写一组对象的行为 XmlAnyElementAttributeUse the XmlAnyElementAttributes to override the behavior of a set of XmlAnyElementAttribute objects. XmlAnyElementAttribute只要每个实例具有不同的属性值,就可以将类的多个实例应用于类成员 Name ; 这将指示将 XmlSerializer 命名元素下的未知元素收集到适当的数组中。Multiple instances of the XmlAnyElementAttribute class can be applied to a class member, as long as each instance has a distinct Name property value; this instructs the XmlSerializer to collect unknown elements under the named element into the appropriate array. 出于此原因,可以将类的多个实例 XmlAnyElementAttribute 添加到中 XmlAnyElementAttributesIt is for this reason that multiple instances of the XmlAnyElementAttribute class can be added to the XmlAnyElementAttributes.

重写一组 XmlAnyElementAttribute 对象:To override a set of XmlAnyElementAttribute objects:

  1. 创建一个 XmlAnyElementAttributesCreate an XmlAnyElementAttributes.

  2. 创建一组 XmlAnyElementAttribute 对象,并使用方法将每个对象添加到集合中 AddCreate the set of XmlAnyElementAttribute objects, and add each object to the collection using the Add method.

  3. 创建一个 XmlAttributesCreate an XmlAttributes.

  4. XmlAnyElements 属性设置为 XmlAnyElementAttributesSet the XmlAnyElements property to the XmlAnyElementAttributes.

  5. 创建一个 XmlAttributeOverridesCreate an XmlAttributeOverrides.

  6. XmlAttributes使用方法将添加到 XmlAttributeOverrides AddAdd the XmlAttributes to the XmlAttributeOverrides using the Add method.

  7. 使用创建的实例 XmlSerializer XmlAttributeOverridesCreate an instance of the XmlSerializer using the XmlAttributeOverrides.

  8. 序列化或反序列化包含对象集的对象 XmlAnyElementAttributeSerialize or deserialize an object that contains the set of XmlAnyElementAttribute objects.

构造函数

XmlAnyElementAttributes()

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

属性

Capacity

获取或设置 CollectionBase 可包含的元素数。Gets or sets the number of elements that the CollectionBase can contain.

(继承自 CollectionBase)
Count

获取 ICollection 中包含的元素数。Gets the number of elements contained in the ICollection.

Count

获取 CollectionBase 实例中包含的元素数。Gets the number of elements contained in the CollectionBase instance. 不能重写此属性。This property cannot be overridden.

(继承自 CollectionBase)
InnerList

获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。Gets an ArrayList containing the list of elements in the CollectionBase instance.

(继承自 CollectionBase)
Item[Int32]

获取或设置指定索引处的 XmlAnyElementAttributeGets or sets the XmlAnyElementAttribute at the specified index.

List

获取一个 IList,它包含 CollectionBase 实例中元素的列表。Gets an IList containing the list of elements in the CollectionBase instance.

(继承自 CollectionBase)

方法

Add(XmlAnyElementAttribute)

XmlAnyElementAttribute 添加到集合中。Adds an XmlAnyElementAttribute to the collection.

Clear()

IList 中移除所有项。Removes all items from the IList.

Clear()

CollectionBase 实例移除所有对象。Removes all objects from the CollectionBase instance. 不能重写此方法。This method cannot be overridden.

(继承自 CollectionBase)
Contains(XmlAnyElementAttribute)

获取一个值,该值指示集合中是否存在指定的 XmlAnyElementAttributeGets a value that indicates whether the specified XmlAnyElementAttribute exists in the collection.

CopyTo(XmlAnyElementAttribute[], Int32)

将整个集合复制到 XmlElementAttribute 对象的一个兼容一维数组,从目标数组的指定索引处开始。Copies the entire collection to a compatible one-dimensional array of XmlElementAttribute objects, starting at the specified index of the target array.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

GetEnumerator()

返回循环访问 CollectionBase 实例的枚举器。Returns an enumerator that iterates through the CollectionBase instance.

(继承自 CollectionBase)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

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

(继承自 Object)
IndexOf(XmlAnyElementAttribute)

获取指定 XmlAnyElementAttribute 的索引。Gets the index of the specified XmlAnyElementAttribute.

Insert(Int32, XmlAnyElementAttribute)

在集合中的指定索引处插入 XmlAnyElementAttributeInserts an XmlAnyElementAttribute into the collection at the specified index.

MemberwiseClone()

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

(继承自 Object)
OnClear()

清除 CollectionBase 实例的内容时执行其他自定义进程。Performs additional custom processes when clearing the contents of the CollectionBase instance.

(继承自 CollectionBase)
OnClearComplete()

在清除 CollectionBase 实例的内容之后执行其他自定义进程。Performs additional custom processes after clearing the contents of the CollectionBase instance.

(继承自 CollectionBase)
OnInsert(Int32, Object)

在向 CollectionBase 实例中插入新元素之前执行其他自定义进程。Performs additional custom processes before inserting a new element into the CollectionBase instance.

(继承自 CollectionBase)
OnInsertComplete(Int32, Object)

在向 CollectionBase 实例中插入新元素之后执行其他自定义进程。Performs additional custom processes after inserting a new element into the CollectionBase instance.

(继承自 CollectionBase)
OnRemove(Int32, Object)

当从 CollectionBase 实例移除元素时执行其他自定义进程。Performs additional custom processes when removing an element from the CollectionBase instance.

(继承自 CollectionBase)
OnRemoveComplete(Int32, Object)

在从 CollectionBase 实例中移除元素之后执行其他自定义进程。Performs additional custom processes after removing an element from the CollectionBase instance.

(继承自 CollectionBase)
OnSet(Int32, Object, Object)

当在 CollectionBase 实例中设置值之前执行其他自定义进程。Performs additional custom processes before setting a value in the CollectionBase instance.

(继承自 CollectionBase)
OnSetComplete(Int32, Object, Object)

当在 CollectionBase 实例中设置值后执行其他自定义进程。Performs additional custom processes after setting a value in the CollectionBase instance.

(继承自 CollectionBase)
OnValidate(Object)

当验证值时执行其他自定义进程。Performs additional custom processes when validating a value.

(继承自 CollectionBase)
Remove(XmlAnyElementAttribute)

从集合中移除指定的 XmlAnyElementAttributeRemoves the specified XmlAnyElementAttribute from the collection.

RemoveAt(Int32)

移除位于指定索引处的 IList 项。Removes the IList item at the specified index.

RemoveAt(Int32)

移除 CollectionBase 实例的指定索引处的元素。Removes the element at the specified index of the CollectionBase instance. 此方法不可重写。This method is not overridable.

(继承自 CollectionBase)
ToString()

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

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

ICollection 的元素复制到数组,从特定索引处开始复制。Copies the elements of the ICollection to an array, starting at a particular index.

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 CollectionBase 复制到兼容的一维 ArrayCopies the entire CollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(继承自 CollectionBase)
ICollection.IsSynchronized

获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。Gets a value indicating whether access to the ICollection is synchronized (thread safe).

ICollection.IsSynchronized

获取一个值,该值指示是否同步对 CollectionBase 的访问(线程安全)。Gets a value indicating whether access to the CollectionBase is synchronized (thread safe).

(继承自 CollectionBase)
ICollection.SyncRoot

获取可用于同步对 ICollection 的访问的对象。Gets an object that can be used to synchronize access to the ICollection.

ICollection.SyncRoot

获取可用于同步对 CollectionBase 的访问的对象。Gets an object that can be used to synchronize access to the CollectionBase.

(继承自 CollectionBase)
IList.Add(Object)

将某项添加到 IList 中。Adds an item to the IList.

IList.Add(Object)

将对象添加到 CollectionBase 的结尾处。Adds an object to the end of the CollectionBase.

(继承自 CollectionBase)
IList.Contains(Object)

确定 IList 是否包含特定值。Determines whether the IList contains a specific value.

IList.Contains(Object)

确定 CollectionBase 是否包含特定元素。Determines whether the CollectionBase contains a specific element.

(继承自 CollectionBase)
IList.IndexOf(Object)

确定 IList 中特定项的索引。Determines the index of a specific item in the IList.

IList.IndexOf(Object)

搜索指定的 Object,并返回整个 CollectionBase 中第一个匹配项的从零开始的索引。Searches for the specified Object and returns the zero-based index of the first occurrence within the entire CollectionBase.

(继承自 CollectionBase)
IList.Insert(Int32, Object)

IList 中的指定索引处插入一个项。Inserts an item to the IList at the specified index.

IList.Insert(Int32, Object)

将元素插入 CollectionBase 的指定索引处。Inserts an element into the CollectionBase at the specified index.

(继承自 CollectionBase)
IList.IsFixedSize

获取一个值,该值指示 IList 是否具有固定大小。Gets a value indicating whether the IList has a fixed size.

IList.IsFixedSize

获取一个值,该值指示 CollectionBase 是否具有固定大小。Gets a value indicating whether the CollectionBase has a fixed size.

(继承自 CollectionBase)
IList.IsReadOnly

获取一个值,该值指示 IList 是否为只读。Gets a value indicating whether the IList is read-only.

IList.IsReadOnly

获取一个值,该值指示 CollectionBase 是否为只读。Gets a value indicating whether the CollectionBase is read-only.

(继承自 CollectionBase)
IList.Item[Int32]

获取或设置指定索引处的元素。Gets or sets the element at the specified index.

IList.Item[Int32]

获取或设置指定索引处的元素。Gets or sets the element at the specified index.

(继承自 CollectionBase)
IList.Remove(Object)

IList 中移除特定对象的第一个匹配项。Removes the first occurrence of a specific object from the IList.

IList.Remove(Object)

CollectionBase 中移除特定对象的第一个匹配项。Removes the first occurrence of a specific object from the CollectionBase.

(继承自 CollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于