XmlAnyElementAttributes 類別

定義

表示 XmlAnyElementAttribute 物件的集合。

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 ,並將其新增至透過 XmlAnyElements 屬性存取的物件集合。 接著,會 XmlAttributes 加入至 XmlAttributeOverrides ,用來建立 XmlSerializerXmlSerializer用來序列化或還原序列化物件。 若要查看使用 XmlAnyElementAttributes 屬性的效果,請在 方法中執行 SerializeObject 方法,以建立名為 UnknownElements.xml 的 Main XML 檔。 編輯產生的檔,以包含其他 (未知) 元素。 批註化 SerializeObject 方法中的 Main 呼叫,並取消批註方法的呼叫 DeserializeObject ,這會列印出任何未知 XML 專案的名稱和值。

#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使用 來覆寫一組 XmlAnyElementAttribute 物件的行為。 只要每個實例都有相異 Name 的屬性值,就可以將類別的 XmlAnyElementAttribute 多個實例套用至類別成員;這會指示 XmlSerializer 將具名元素底下的未知元素收集到適當的陣列中。 基於這個理由,可以將 類別的 XmlAnyElementAttribute 多個實例新增至 XmlAnyElementAttributes

若要覆寫一組 XmlAnyElementAttribute 物件:

  1. 建立 XmlAnyElementAttributes

  2. 建立 物件集 XmlAnyElementAttribute ,並使用 Add 方法將每個物件新增至集合。

  3. 建立 XmlAttributes

  4. XmlAnyElements 屬性設定為 XmlAnyElementAttributes

  5. 建立 XmlAttributeOverrides

  6. 使用 方法將 XmlAttributes 新增至 XmlAttributeOverridesAdd

  7. 使用 XmlAttributeOverrides 建立 的 XmlSerializer 實例。

  8. 序列化或還原序列化包含 物件集合 XmlAnyElementAttribute 的物件。

建構函式

XmlAnyElementAttributes()

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

屬性

Capacity

取得或設定 CollectionBase 可包含的項目數目。

(繼承來源 CollectionBase)
Count

取得 ICollection 中所包含的項目數。

Count

取得 CollectionBase 執行個體中包含的元素數目。 這個屬性無法覆寫。

(繼承來源 CollectionBase)
InnerList

取得包含 ArrayList 執行個體中之元素清單的 CollectionBase

(繼承來源 CollectionBase)
Item[Int32]

取得或設定在指定索引處的 XmlAnyElementAttribute

List

取得包含 IList 執行個體中之元素清單的 CollectionBase

(繼承來源 CollectionBase)

方法

Add(XmlAnyElementAttribute)

XmlAnyElementAttribute 加入至集合。

Clear()

IList 中移除所有項目。

Clear()

CollectionBase 執行個體移除所有的物件。 無法覆寫這個方法。

(繼承來源 CollectionBase)
Contains(XmlAnyElementAttribute)

取得值,指出指定 XmlAnyElementAttribute 是否存在於集合中。

CopyTo(XmlAnyElementAttribute[], Int32)

從目標陣列的指定索引開始,複製整個集合至 XmlElementAttribute 物件的相容一維陣列。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回逐一查看集合的列舉值。

GetEnumerator()

傳回可逐一查看 CollectionBase 執行個體的列舉值。

(繼承來源 CollectionBase)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(XmlAnyElementAttribute)

取得指定 XmlAnyElementAttribute 的索引。

Insert(Int32, XmlAnyElementAttribute)

XmlAnyElementAttribute 插入集合中指定的索引處。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnClear()

在清除 CollectionBase 執行個體的內容之後,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnClearComplete()

在清除 CollectionBase 執行個體的內容後,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnInsert(Int32, Object)

在將新的元素插入至 CollectionBase 執行個體前,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnInsertComplete(Int32, Object)

在將新的元素插入至 CollectionBase 執行個體後,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnRemove(Int32, Object)

當從 CollectionBase 執行個體移除元素時,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnRemoveComplete(Int32, Object)

在從 CollectionBase 執行個體移除元素後,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnSet(Int32, Object, Object)

CollectionBase 執行個體中設定數值前,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnSetComplete(Int32, Object, Object)

CollectionBase 執行個體中設定數值後,執行額外的自訂處理序。

(繼承來源 CollectionBase)
OnValidate(Object)

當驗證數值時,執行額外的自訂處理序。

(繼承來源 CollectionBase)
Remove(XmlAnyElementAttribute)

從集合移除指定的 XmlAnyElementAttribute

RemoveAt(Int32)

移除在指定索引處的 IList 項目。

RemoveAt(Int32)

移除 CollectionBase 執行個體之指定索引的元素。 這個方法不可覆寫。

(繼承來源 CollectionBase)
ToString()

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

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的索引開始,將 ICollection 的元素複製到陣列。

ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 CollectionBase 複製到相容的一維 Array

(繼承來源 CollectionBase)
ICollection.IsSynchronized

取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。

ICollection.IsSynchronized

取得值,這個值表示對 CollectionBase 的存取是否同步 (安全執行緒)。

(繼承來源 CollectionBase)
ICollection.SyncRoot

取得可用以同步存取 ICollection 的物件。

ICollection.SyncRoot

取得可用以同步存取 CollectionBase 的物件。

(繼承來源 CollectionBase)
IList.Add(Object)

將項目加入至 IList

IList.Add(Object)

將物件加入至 CollectionBase 的末端。

(繼承來源 CollectionBase)
IList.Contains(Object)

判斷 IList 是否包含特定值。

IList.Contains(Object)

判斷 CollectionBase 是否包含特定項目。

(繼承來源 CollectionBase)
IList.IndexOf(Object)

判斷 IList 中指定項目的索引。

IList.IndexOf(Object)

搜尋指定的 Object,並傳回在整個 CollectionBase 中第一個符合項目之以零為起始的索引。

(繼承來源 CollectionBase)
IList.Insert(Int32, Object)

將項目插入位於指定索引的 IList

IList.Insert(Int32, Object)

將項目插入至 CollectionBase 中指定的索引位置。

(繼承來源 CollectionBase)
IList.IsFixedSize

取得值,指出 IList 是否有固定的大小。

IList.IsFixedSize

取得值,指出 CollectionBase 是否有固定的大小。

(繼承來源 CollectionBase)
IList.IsReadOnly

取得值,指出 IList 是否唯讀。

IList.IsReadOnly

取得值,指出 CollectionBase 是否唯讀。

(繼承來源 CollectionBase)
IList.Item[Int32]

在指定的索引位置上取得或設定項目。

IList.Item[Int32]

在指定的索引位置上取得或設定項目。

(繼承來源 CollectionBase)
IList.Remove(Object)

IList 移除特定物件之第一個符合的元素。

IList.Remove(Object)

CollectionBase 移除特定物件之第一個符合的元素。

(繼承來源 CollectionBase)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於