Specifies that the member (a field that returns an array of System.Xml.XmlElement or System.Xml.XmlNode objects) contains objects that represent any XML element that has no corresponding member in the object being serialized or deserialized.
See Also: XmlAnyElementAttribute Members
When 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 System.Xml.Linq.XElement or System.Xml.Linq.XNode in place of System.Xml.XmlElement and System.Xml.XmlNode.
Use the System.Xml.Serialization.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.
Apply the System.Xml.Serialization.XmlAnyElementAttribute to a field that returns an array of System.Xml.XmlElement or System.Xml.XmlNode objects. Such a field can be used in two ways, depending on whether an object is being serialized or deserialized. 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. If you specify a XmlAnyElementAttribute.Name property value when applying the attribute, all System.Xml.XmlElement or System.Xml.XmlNode objects inserted into the array must have the same element name and default namespace, or an exception is thrown. If you set the XmlAnyElementAttribute.Namespace property value, you must set the XmlAnyElementAttribute.Name property as well, and the System.Xml.XmlElement or System.Xml.XmlNode objects must also have the same name and namespace values. If no XmlAnyElementAttribute.Name value is specified, the System.Xml.XmlElement or System.Xml.XmlNode objects can have any element name.
When you call the XmlSerializer.Deserialize(System.IO.Stream) method of the System.Xml.Serialization.XmlSerializer class, all elements that do not have a corresponding member in the object being deserialized are collected in the array. After deserialization, iterate through the collection of System.Xml.XmlElement items to process the data. If you specify a XmlAnyElementAttribute.Name value, the array contains only XML elements with that name. If you do not specify a XmlAnyElementAttribute.Name value, the array contains all elements that have no corresponding member in the class. If a class contains more than one field to which the attribute is applied, use the XmlAnyElementAttribute.Name, or XmlAnyElementAttribute.Name and XmlAnyElementAttribute.Namespace properties to differentiate between the contents of the arrays. If such a class (with multiple fields) also contains one field that has no differentiating property values set (in other words, XmlAnyElementAttribute.Name and XmlAnyElementAttribute.Namespace) during deserialization, this array contains any unknown XML elements that are not already contained in the other arrays. If a class contains more than one field that does not have a differentiating XmlAnyElementAttribute.Name, or XmlAnyElementAttribute.Name and XmlAnyElementAttribute.Namespace value set, the behavior during deserialization is unspecified.
You can also apply the System.Xml.Serialization.XmlAnyElementAttribute to a field that returns a single System.Xml.XmlElement object. If you do so, you must use the properties and methods of the System.Xml.XmlElement class to recursively iterate through the unknown elements.
You can apply multiple instances of the System.Xml.Serialization.XmlAnyElementAttribute to a class member, but each instance must have a distinct XmlAnyElementAttribute.Name property value. Or, if the same XmlAnyElementAttribute.Name property is set for each instance, a distinct XmlAnyElementAttribute.Namespace property value must be set for each instance.
The XmlSerializer.UnknownNode and XmlSerializer.UnknownAttribute events of the System.Xml.Serialization.XmlSerializer do not occur if you apply the System.Xml.Serialization.XmlAnyElementAttribute to a member of a class.
You can use the word XmlAnyElement in your code instead of the longer System.Xml.Serialization.XmlAnyElementAttribute.
For more information about using attributes, see Extending Metadata Using Attributes.