[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)] |
Apply the XmlAnyAttributeAttribute to a field that returns an array of XmlAttribute or XmlNode objects. When the XmlSerializer.Deserialize method of the XmlSerializer class is called, all attributes that do not have a corresponding member in the class being deserialized will be collected in the array. After deserialization, you can iterate through the collection of XmlAttribute items to process the data.
The XmlSerializer.UnknownNode and XmlSerializer.UnknownAttribute events of the XmlSerializer will not occur if you apply the XmlAnyAttributeAttribute to a member of a class.
For more information about using attributes, see the conceptual topic at MSDN: extendingmetadatausingattributes.
<?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(); } }
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Constructs a new instance of the XmlAnyAttributeAttribute class. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XmlAnyAttributeAttribute(); |
<?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 Things array will be used to collect all unknown // attributes found when deserializing. public XmlAttribute[]Things; } public class Test{ static void Main(){ Test t = new Test(); t.DeserializeObject("UnknownAttributes.xml"); } private void DeserializeObject(string filename){ // Use the CreateOverrideSerializer to return an instance // of the XmlSerializer customized for overrides. XmlSerializer ser = CreateOverrideSerializer(); // A FileStream is needed to read the XML document. FileStream fs = new FileStream(filename, FileMode.Open); Group g = (Group) ser.Deserialize(fs); fs.Close(); Console.WriteLine(g.GroupName); Console.WriteLine(g.Things.Length); foreach(XmlAttribute xAtt in g.Things){ Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml); } } private XmlSerializer CreateOverrideSerializer(){ // Override the Things field to capture all // unknown XML attributes. XmlAnyAttributeAttribute myAnyAttribute = new XmlAnyAttributeAttribute(); XmlAttributeOverrides xOverride = new XmlAttributeOverrides(); XmlAttributes xAtts = new XmlAttributes(); xAtts.XmlAnyAttribute=myAnyAttribute; xOverride.Add(typeof(Group), "Things", xAtts); return new XmlSerializer(typeof(Group) , xOverride); } }
public virtual object TypeId {get;}
|
~XmlAnyAttributeAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |