Export (0) Print
Expand All

XmlAnyElementAttribute Class

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)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=True)> _
Public Class XmlAnyElementAttribute
	Inherits Attribute
'Usage
Dim instance As XmlAnyElementAttribute

/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true) */ 
public class XmlAnyElementAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true) 
public class XmlAnyElementAttribute extends Attribute
Not applicable.

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 Name 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 Namespace property value, you must set the Name property as well, and the XmlElement or XmlNode objects must also have the same name and namespace values. If no Name 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 Name value, the array contains only XML elements with that name. If you do not specify a 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 Name, or Name and 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, Name and 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 Name, or Name and Namespace 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 Name property value. Or, if the same Name property is set for each instance, a distinct Namespace 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.

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 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 class XClass
{
    /* Apply the XmlAnyElementAttribute to a field returning an array
       of XmlElement objects. 
    */
    /** @attribute XmlAnyElement()
     */
    public XmlElement allElements[];
} //XClass

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.DeserializeObject("XFile.xml");
    } //main

    private void DeserializeObject(String fileName)
    {
        // Create an XmlSerializer.
        XmlSerializer mySerializer = new XmlSerializer(XClass.class.ToType());

        // 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.
        for (int iCtr = 0; iCtr < x.allElements.length; iCtr++) {
            XmlElement xel = (XmlElement)x.allElements.get_Item(iCtr);
            Console.WriteLine(xel.get_LocalName() + ": " + xel.get_Value());
        }
    } //DeserializeObject
} //Test

System.Object
   System.Attribute
    System.Xml.Serialization.XmlAnyElementAttribute

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft