XmlAnyElementAttribute Class
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Specifies that the member (a field that returns an array of XmlElement or XmlNode objects) contains objects that represent any XML element that has no corresponding member in the object being serialized or deserialized.

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

SystemObject
   SystemAttribute
    System.Xml.SerializationXmlAnyElementAttribute

[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, 
	AllowMultiple = true)]
public class XmlAnyElementAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property | AttributeTargets::Field | AttributeTargets::Parameter | AttributeTargets::ReturnValue, 
	AllowMultiple = true)]
public ref class XmlAnyElementAttribute : Attribute
[<AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue,
    AllowMultiple = true)>]
type XmlAnyElementAttribute = 
    class
        inherit Attribute
    end
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue,
	AllowMultiple := True)>
Public Class XmlAnyElementAttribute
	Inherits Attribute
NameDescription
System_CAPS_pubmethod XmlAnyElementAttribute

Initializes a new instance of the XmlAnyElementAttribute class.

System_CAPS_pubmethod XmlAnyElementAttribute

Initializes a new instance of the XmlAnyElementAttribute class and specifies the XML element name generated in the XML document.

System_CAPS_pubmethod XmlAnyElementAttribute

Initializes a new instance of the XmlAnyElementAttribute class and specifies the XML element name generated in the XML document and its XML namespace.

NameDescription
System_CAPS_pubproperty

Gets or sets the XML element name.

System_CAPS_pubproperty

Gets or sets the XML namespace generated in the XML document.

System_CAPS_pubproperty

Gets or sets the explicit order in which the elements are serialized or deserialized.

System_CAPS_pubproperty

When implemented in a derived class, gets a unique identifier for this Attribute.(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethod Equals

This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode

Returns the hash code for this instance.(Inherited from Attribute.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod IsDefaultAttribute

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.(Inherited from Attribute.)

System_CAPS_pubmethod Match

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetIDsOfNames

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfo

Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfoCount

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeInvoke

Provides access to properties and methods exposed by an object.(Inherited from Attribute.)

System_CAPS_tipTip

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 XElement or XNode in place of XmlElement and XmlNode.

Use the 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 XmlAnyElementAttribute to a field that returns an array of XmlElement or 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 property value when applying the attribute, all XmlElement or XmlNode objects inserted into the array must have the same element name and default namespace, or an exception is thrown. If you set the property value, you must set the property as well, and the XmlElement or XmlNode objects must also have the same name and namespace values. If no value is specified, the XmlElement or XmlNode objects can have any element name.

When you call the Deserialize method of the 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 XmlElement items to process the data. If you specify a value, the array contains only XML elements with that name. If you do not specify a 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 , or and 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, and ) 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 , or and value set, the behavior during deserialization is unspecified.

You can also apply the XmlAnyElementAttribute to a field that returns a single XmlElement object. If you do so, you must use the properties and methods of the XmlElement class to recursively iterate through the unknown elements.

You can apply multiple instances of the XmlAnyElementAttribute to a class member, but each instance must have a distinct property value. Or, if the same property is set for each instance, a distinct property value must be set for each instance.

The UnknownNode and UnknownAttribute events of the XmlSerializer do not occur if you apply the XmlAnyElementAttribute to a member of a class.

System_CAPS_noteNote

You can use the word XmlAnyElement in your code instead of the longer XmlAnyElementAttribute.

For more information about using attributes, see Extending Metadata Using Attributes.

The following example applies the XmlAnyElementAttribute to a field named AllElements that returns an array of XmlElement objects.

public class XClass
{
   /* Apply the XmlAnyElementAttribute to a field returning an array
   of XmlElement objects. */
   [XmlAnyElement]
   public XmlElement[] AllElements;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.DeserializeObject("XFile.xml");
   }

   private void DeserializeObject(string filename)
   {
      // Create an XmlSerializer.
      XmlSerializer mySerializer = new XmlSerializer(typeof(XClass));

      // To read a file, a FileStream is needed.
      FileStream fs = new FileStream(filename, FileMode.Open);

      // Deserialize the class.
      XClass x = (XClass) mySerializer.Deserialize(fs);

      // Read the element names and values.
      foreach(XmlElement xel in x.AllElements)
         Console.WriteLine(xel.LocalName + ": " + xel.Value);
   }
}
Public Class XClass
    ' Apply the XmlAnyElementAttribute to a field returning an array
    ' of XmlElement objects.
    <XmlAnyElement()> Public AllElements() As XmlElement
End Class 'XClass


Public Class Test

    Public Shared Sub Main()
        Dim t As New Test()
        t.DeserializeObject("XFile.xml")
    End Sub 'Main


    Private Sub DeserializeObject(filename As String)
        ' Create an XmlSerializer.
        Dim mySerializer As New XmlSerializer(GetType(XClass))

        ' To read a file, a FileStream is needed.
        Dim fs As New FileStream(filename, FileMode.Open)

        ' Deserialize the class.
        Dim x As XClass = CType(mySerializer.Deserialize(fs), XClass)

        ' Read the element names and values.
        Dim xel As XmlElement
        For Each xel In  x.AllElements
            Console.WriteLine((xel.LocalName & ": " & xel.Value))
        Next xel
    End Sub 'DeserializeObject
End Class 'Test 
public ref class XClass
{
public:

   /* Apply the XmlAnyElementAttribute to a field returning an array
      of XmlElement objects. */

   [XmlAnyElement]
   array<XmlElement^>^AllElements;
};

public ref class Test
{
public:
   void DeserializeObject( String^ filename )
   {
      // Create an XmlSerializer.
      XmlSerializer^ mySerializer = gcnew XmlSerializer( XClass::typeid );

      // To read a file, a FileStream is needed.
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );

      // Deserialize the class.
      XClass^ x = dynamic_cast<XClass^>(mySerializer->Deserialize( fs ));

      // Read the element names and values.
      System::Collections::IEnumerator^ myEnum = x->AllElements->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         XmlElement^ xel = safe_cast<XmlElement^>(myEnum->Current);
         Console::WriteLine( "{0}: {1}", xel->LocalName, xel->Value );
      }
   }
};

int main()
{
   Test^ t = gcnew Test;
   t->DeserializeObject( "XFile.xml" );
}
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top