Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlAnyAttributeAttribute Class

Specifies that the member (a field that returns an array of XmlAttribute objects) can contain any XML attributes.

System.Object
  System.Attribute
    System.Xml.Serialization.XmlAnyAttributeAttribute

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple = false)]
public class XmlAnyAttributeAttribute : Attribute

The XmlAnyAttributeAttribute type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsXmlAnyAttributeAttributeConstructs a new instance of the XmlAnyAttributeAttribute class.
Top
  NameDescription
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top
  NameDescription
Public methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

Use the XmlAnyAttributeAttribute to contain arbitrary data (as XML attributes) that is sent as part of an XML document, such as, metadata sent as part of the document.

Apply the XmlAnyAttributeAttribute to a field that returns an array of XmlAttribute or XmlNode objects. When the Deserialize method of the XmlSerializer class is called, all XML attributes that do not have a corresponding member in the class being deserialized are collected in the array. After deserialization, you can iterate through the collection of XmlAttribute items to process the data.

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

NoteNote

You can use the word XmlAnyAttribute in your code instead of the longer XmlAnyAttributeAttribute.

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

The following example collects all unknown attributes into an array of XmlAttribute objects. To try the example, create a file named UnknownAttributes.xml that contains the following XML:

 <?xml version="1.0" encoding="utf-8"?>
 <Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>
   <GroupName>MyGroup</GroupName>
 </Group>
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   // The UnknownAttributes array will be used to collect all unknown 
   // attributes found when deserializing.
   [XmlAnyAttribute]
    public XmlAttribute[]XAttributes;
}

public class Test{
   static void Main(){
      Test t = new Test();
      // Deserialize the file containing unknown attributes.

      t.DeserializeObject("UnknownAttributes.xml");
   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // A FileStream is needed to read the XML document.
     FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group) ser.Deserialize(fs);
     fs.Close();
     // Write out the data, including unknown attributes.
     Console.WriteLine(g.GroupName);
     Console.WriteLine("Number of unknown attributes: " + 
     g.XAttributes.Length);
     foreach(XmlAttribute xAtt in g.XAttributes){
     Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
     }
     // Serialize the object again with the attributes added. 
     this.SerializeObject("AttributesAdded.xml",g);
   }

   private void SerializeObject(string filename, object g){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, g);
      writer.Close();
   }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.