This documentation is archived and is not being maintained.

XmlAnyElementAttributes Class

Represents a collection of XmlAnyElementAttribute objects.

For a list of all members of this type, see XmlAnyElementAttributes Members.


[Visual Basic]
Public Class XmlAnyElementAttributes
   Inherits CollectionBase
public class XmlAnyElementAttributes : CollectionBase
public __gc class XmlAnyElementAttributes : public CollectionBase
public class XmlAnyElementAttributes extends CollectionBase

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Use the XmlAnyElementAttributes to override the behavior of a set of XmlAnyElementAttribute objects. 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. It is for this reason that multiple instances of the XmlAnyElementAttribute class can be added to the XmlAnyElementAttributes.

To override a set of XmlAnyElementAttribute objects:

  1. Create an XmlAnyElementAttributes.
  2. Create the set of XmlAnyElementAttribute objects, and add each object to the collection using the Add method.
  3. Create an XmlAttributes.
  4. Set the XmlAnyElements property to the XmlAnyElementAttributes.
  5. Create an XmlAttributeOverrides.
  6. Add the XmlAttributes to the XmlAttributeOverrides using the Add method.
  7. Create an instance of the XmlSerializer using the XmlAttributeOverrides.
  8. Serialize or deserialize an object that contains the set of XmlAnyElementAttribute objects.


[Visual Basic, C#, C++] The following example creates a new XmlAnyElementAttribute and adds it to the collection of objects accessed through the XmlAnyElements property. The XmlAttributes is then added to a XmlAttributeOverrides which is used to create an XmlSerializer. The XmlSerializer is used to serialize or deserialize an object. To 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. Comment out the SerializeObject call in the Main method, and uncomment the call to the DeserializeObject method, which will print out the name and value of any unknown XML element.

[Visual Basic] 
Imports System
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.
   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)
   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)
     Dim xelement As XmlELement
     for each xelement in g.Things
        Console.WriteLine(xelement.Name &": " & xelement.InnerXml)
   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()
      xOverride.Add(GetType(Group), "Things", xAtts)
      return New XmlSerializer(GetType(Group) , xOverride)
   End Function
End Class

using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   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.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);

   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)
     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();
      xOverride.Add(typeof(Group), "Things", xAtts);
      return new XmlSerializer(typeof(Group) , xOverride);


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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;

public __gc class Group
   String* GroupName;

   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.

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

void SerializeObject(String* filename)
   XmlSerializer* ser = new XmlSerializer( __typeof(Group));
   TextWriter* writer = new StreamWriter(filename);
   Group* g = new Group();
   g->GroupName = S"MyGroup";
   ser->Serialize(writer, g);

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 = __try_cast<Group*>( ser->Deserialize(fs ));
   for ( int i = 0; i < g->Things->Length; ++i )
      XmlElement* xelement = __try_cast<XmlElement*>( g->Things[i] );
      Console::WriteLine( S"{0}: {1}", xelement->Name, xelement->InnerXml );

XmlSerializer* CreateOverrideSerializer()
   XmlAnyElementAttribute* myAnyElement = new XmlAnyElementAttribute();
   XmlAttributeOverrides* xOverride = new XmlAttributeOverrides();
   XmlAttributes* xAtts = new XmlAttributes();
   xOverride->Add(__typeof(Group), S"Things", xAtts);
   return new XmlSerializer(__typeof(Group) , xOverride);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlAnyElementAttributes Members | System.Xml.Serialization Namespace