[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)] |
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 XmlAnyElementAttribute.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 XmlAnyElementAttribute.Namespace property value, you must set the XmlAnyElementAttribute.Name property as well, and the XmlElement or XmlNode objects must also have the same name and namespace values. If no XmlAnyElementAttribute.Name value is specified, the XmlElement or XmlNode objects can have any element name.
When you call the XmlSerializer.Deserialize method of the XmlSerializer class, all attributes 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 XmlAnyElementAttribute.Name value, the array will contain only XML elements with that name. If you do not specify a XmlAnyElementAttribute.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 XmlAnyElementAttribute.Name, or XmlAnyElementAttribute.Name and XmlAnyElementAttribute.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, XmlAnyElementAttribute.Name and XmlAnyElementAttribute.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 XmlAnyElementAttribute.Name, or XmlAnyElementAttribute.Name and XmlAnyElementAttribute.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 XmlAnyElementAttribute.Name property value. Or, if the same XmlAnyElementAttribute.Name property is set for each instance, a distinct XmlAnyElementAttribute.Namespace property value must be set for each instance.
The XmlSerializer.UnknownNode and XmlSerializer.UnknownAttribute events of the XmlSerializer will not occur if you apply the XmlAnyElementAttribute to a member of a class.
For more information about using attributes, see the conceptual topic at MSDN: extendingmetadatausingattributes.
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); } }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the XmlAnyElementAttribute class. |
ctor #2 | Overloaded:.ctor(string name) Initializes a new instance of the XmlAnyElementAttribute class; specifies the XML element name generated in the XML document. |
ctor #3 | Overloaded:.ctor(string name, string ns) Initializes a new instance of the XmlAnyElementAttribute class; specifies the XML element name generated in the XML document and its XML namespace. |
Name | Read-write Gets or sets the XML element name. |
Namespace | Read-write Gets or sets the XML namespace generated in the XML document. |
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 XmlAnyElementAttribute(); |
public XmlAnyElementAttribute( |
name
name
ns
public string Name {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The element name of an array member does not match the element name specified by the XmlAnyElementAttribute.Name property. |
When you call the XmlSerializer.Deserialize method of the XmlSerializer class, all attributes that do not have a corresponding member in the object being deserialized will be collected in the array. If you specify a XmlAnyElementAttribute.Name value, the array will contain only XML elements with that name. If you do not specify a XmlAnyElementAttribute.Name value, the array will contain all elements that have no corresponding member in the class. If a class contains more than one field that to which the attribute is applied, you should use the XmlAnyElementAttribute.Name and XmlAnyElementAttribute.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 (that is, XmlAnyElementAttribute.Name and XmlAnyElementAttribute.Namespace) during deserialization, the array will contain any XML elements that are not already contained in the other arrays. If you add more than one field that doesn't have a differentiating XmlAnyElementAttribute.Name or XmlAnyElementAttribute.Namespace value set, the last field in the class will contain all unknown elements that are not already contained in the other arrays, and any other fields will be set to null.
You can apply multiple instances of the XmlAnyElementAttribute to a class member, but each instance must have a distinct XmlAnyElementAttribute.Name property value. Or, if the same XmlAnyElementAttribute.Name property is set for each instance, a distinct XmlAnyElementAttribute.Namespace property value must be set for each instance.
using System; using System.Text; using System.IO; using System.Xml.Serialization; using System.Xml; using System.Xml.Schema; [XmlRoot(Namespace = "http://www.cohowinery.com")] public class Group{ public string GroupName; // This is for serializing Employee elements. [XmlAnyElement(Name = "Employee")] public XmlElement[] UnknownEmployees; // This is for serializing City elements. [XmlAnyElement (Name = "City", Namespace = "http://www.cpandl.com")] public XmlElement[] UnknownCity; // This one is for all other unknown elements. [XmlAnyElement] public XmlElement[] UnknownElements; } public class Test{ static void Main(){ Test t = new Test(); t.SerializeObject("AnyElementArray.xml"); t.DeserializeObject("AnyElementArray.xml"); Console.WriteLine("Done"); } private void SerializeObject(string filename){ XmlSerializer ser = new XmlSerializer(typeof(Group)); // Create an XmlNamespaces to use. XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add("c", "http://www.cohowinery.com"); namespaces.Add("i", "http://www.cpandl.com"); Group myGroup = new Group(); // Create arrays of arbitrary XmlElement objects. // First create an XmlDocument, used to create the // XmlElement objects. XmlDocument xDoc = new XmlDocument(); // Create an array of Employee XmlElement objects. XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El1.InnerText = "John"; XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El2.InnerText = "Joan"; XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El3.InnerText = "Jim"; myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3}; // Create an array of City XmlElement objects. XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf1.InnerText = "Tokyo"; XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf2.InnerText = "New York"; XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf3.InnerText = "Rome"; myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3}; XmlElement xEl1 = xDoc.CreateElement("bld"); xEl1.InnerText = "42"; XmlElement xEl2 = xDoc.CreateElement("Region"); xEl2.InnerText = "West"; XmlElement xEl3 = xDoc.CreateElement("type"); xEl3.InnerText = "Technical"; myGroup.UnknownElements = new XmlElement[]{xEl1,xEl2,xEl3}; // Serialize the class, and close the TextWriter. TextWriter writer = new StreamWriter(filename); ser.Serialize(writer, myGroup, namespaces); writer.Close(); } private void DeserializeObject(string filename){ XmlSerializer ser = new XmlSerializer(typeof(Group)); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup; myGroup = (Group)ser.Deserialize(fs); fs.Close(); foreach(XmlElement xEmp in myGroup.UnknownEmployees){ Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);} foreach(XmlElement xCity in myGroup.UnknownCity){ Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);} foreach(XmlElement xEl in myGroup.UnknownElements){ Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);} } }
public string Namespace {get; set;}
|
To set the XmlAnyElementAttribute.Namespace property to a prefixed name, create an XmlSerializerNamespaces that contains the namespaces and prefixes used in the XML document. Set the XmlAnyElementAttribute.Namespace property to one of the namespaces in the XmlSerializerNamespaces. When the XML is generated, the attribute name will be correctly prefixed with the prefix associated with the specified namespace.
using System; using System.Text; using System.IO; using System.Xml.Serialization; using System.Xml; using System.Xml.Schema; [XmlRoot(Namespace = "http://www.cohowinery.com")] public class Group{ public string GroupName; // This is for serializing Employee elements. [XmlAnyElement(Name = "Employee")] public XmlElement[] UnknownEmployees; // This is for serializing City elements. [XmlAnyElement (Name = "City", Namespace = "http://www.cpandl.com")] public XmlElement[] UnknownCity; // This one is for all other unknown elements. [XmlAnyElement] public XmlElement[] UnknownElements; } public class Test{ static void Main(){ Test t = new Test(); t.SerializeObject("AnyElementArray.xml"); t.DeserializeObject("AnyElementArray.xml"); Console.WriteLine("Done"); } private void SerializeObject(string filename){ XmlSerializer ser = new XmlSerializer(typeof(Group)); // Create an XmlNamespaces to use. XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add("c", "http://www.cohowinery.com"); namespaces.Add("i", "http://www.cpandl.com"); Group myGroup = new Group(); // Create arrays of arbitrary XmlElement objects. // First create an XmlDocument, used to create the // XmlElement objects. XmlDocument xDoc = new XmlDocument(); // Create an array of Employee XmlElement objects. XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El1.InnerText = "John"; XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El2.InnerText = "Joan"; XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com"); El3.InnerText = "Jim"; myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3}; // Create an array of City XmlElement objects. XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf1.InnerText = "Tokyo"; XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf2.InnerText = "New York"; XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com"); inf3.InnerText = "Rome"; myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3}; XmlElement xEl1 = xDoc.CreateElement("bld"); xEl1.InnerText = "42"; XmlElement xEl2 = xDoc.CreateElement("Region"); xEl2.InnerText = "West"; XmlElement xEl3 = xDoc.CreateElement("type"); xEl3.InnerText = "Technical"; myGroup.UnknownElements = new XmlElement[]{xEl1,xEl2,xEl3}; // Serialize the class, and close the TextWriter. TextWriter writer = new StreamWriter(filename); ser.Serialize(writer, myGroup, namespaces); writer.Close(); } private void DeserializeObject(string filename){ XmlSerializer ser = new XmlSerializer(typeof(Group)); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup; myGroup = (Group)ser.Deserialize(fs); fs.Close(); foreach(XmlElement xEmp in myGroup.UnknownEmployees){ Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);} foreach(XmlElement xCity in myGroup.UnknownCity){ Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);} foreach(XmlElement xEl in myGroup.UnknownElements){ Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);} } }
public virtual object TypeId {get;}
|
~XmlAnyElementAttribute(); |
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(); |