Export (0) Print
Expand All

XmlAnyElementAttribute Class

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

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

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

[Visual Basic]
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlAnyElementAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlAnyElementAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
   AttributeTargets::ReturnValue)]
public __gc class XmlAnyElementAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlAnyElementAttribute extends Attribute

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.

Remarks

Use the XmlAnyElementAttribute to contain arbitrary data (as XML elements) that might 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 will be 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 will be 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 will be 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 will contain only XML elements with that name. If you do not specify a Name value, the array will contain all elements that have no corresponding member in the class. If a class contains more than one field to which the attribute is applied, you should 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 will contain any unknown XML elements that are not already contained in the other arrays. If a class contains more than one field that doesn't 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 will have to 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 will not occur if you apply the XmlAnyElementAttribute to a member of a class.

Note   In your code, you can use the word XmlAnyElement instead of the longer XmlAnyElementAttribute.

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

Example

[Visual Basic, C#, C++] The following example applies the XmlAnyElementAttribute to a field named AllElements that returns an array of XmlElement objects.

[Visual Basic] 
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 

[C#] 
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);
   }
}

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

public __gc class Test
{
public:
   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 = dynamic_cast<XClass*> (mySerializer->Deserialize(fs));

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

int main()
{
   Test* t = new Test();
   t->DeserializeObject(S"XFile.xml");
}

[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.

Requirements

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, .NET Compact Framework

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

See Also

XmlAnyElementAttribute Members | System.Xml.Serialization Namespace | XmlAnyAttributeAttribute | Introducing XML Serialization

Show:
© 2014 Microsoft