Export (0) Print
Expand All

XmlAttributes.XmlAnyElements Property

Gets the collection of XmlAnyElementAttribute objects to override.

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

'Declaration
Public ReadOnly Property XmlAnyElements As XmlAnyElementAttributes
'Usage
Dim instance As XmlAttributes 
Dim value As XmlAnyElementAttributes 

value = instance.XmlAnyElements

Property Value

Type: System.Xml.Serialization.XmlAnyElementAttributes
An XmlAnyElementAttributes object that represents the collection of XmlAnyElementAttribute objects.

The XmlAnyElementAttribute can be applied to a member that returns an array of XmlElement objects on deserialization. This allows the XmlSerializer to deserialize any elements that do not have a corresponding member in the object being deserialized--thus those elements are "unknown" to the XmlSerializer. This is useful when the XML stream has been altered by an XML Web service, or when it is known that random data is always included with the XML stream.

The XmlAnyElements property allows you to override the serialization of a member to function as a member to which the XmlAnyElementAttribute has been applied.

The following example creates a new XmlAnyElementAttribute object and adds it to the collection of objects accessed through the XmlAnyElements property. The XmlAttributes object is then added to a XmlAttributeOverrides object which is used to create an XmlSerializer object. 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 prints out the name and value of any unknown XML element.

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.
     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
#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
{
public:
   String* GroupName;

   [XmlAnyElement]
   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(S"UnknownElements.xml");

   // 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);
   writer->Close();
}


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 ));
   fs->Close();
   Console::WriteLine(g->GroupName);
   Console::WriteLine(g->Things->Length);
   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();
   xAtts->XmlAnyElements->Add(myAnyElement);
   xOverride->Add(__typeof(Group), S"Things", xAtts);
   return new XmlSerializer(__typeof(Group) , xOverride);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft