|
80 | 80 |
|
81 | 81 | ## Remarks |
82 | 82 |
|
83 | | -> [!TIP] |
84 | | -> When working in a portable class library, such as in Silverlight, Windows Phone or Windows Store App project, and you are using the .NET Framework 4.0.3 and above, use <xref:System.Xml.Linq.XElement> or <xref:System.Xml.Linq.XNode> in place of <xref:System.Xml.XmlElement> and <xref:System.Xml.XmlNode>. |
| 83 | +Use the <xref:System.Xml.Serialization.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. |
85 | 84 |
|
86 | | - Use the <xref:System.Xml.Serialization.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. |
| 85 | +Apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a field that returns an array of <xref:System.Xml.XmlElement> or <xref:System.Xml.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property value when applying the attribute, all <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects inserted into the array must have the same element name and default namespace, or an exception is thrown. If you set the <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> property value, you must set the <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property as well, and the <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects must also have the same name and namespace values. If no <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> value is specified, the <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects can have any element name. |
87 | 86 |
|
88 | | - Apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a field that returns an array of <xref:System.Xml.XmlElement> or <xref:System.Xml.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property value when applying the attribute, all <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects inserted into the array must have the same element name and default namespace, or an exception is thrown. If you set the <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> property value, you must set the <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property as well, and the <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects must also have the same name and namespace values. If no <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> value is specified, the <xref:System.Xml.XmlElement> or <xref:System.Xml.XmlNode> objects can have any element name. |
| 87 | +When you call the <xref:System.Xml.Serialization.XmlSerializer.Deserialize*> method of the <xref:System.Xml.Serialization.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 <xref:System.Xml.XmlElement> items to process the data. If you specify a <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> value, the array contains only XML elements with that name. If you do not specify a <xref:System.Xml.Serialization.XmlAnyElementAttribute.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name>, or <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.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, <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.XmlAnyElementAttribute.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name>, or <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> value set, the behavior during deserialization is unspecified. |
89 | 88 |
|
90 | | - When you call the <xref:System.Xml.Serialization.XmlSerializer.Deserialize*> method of the <xref:System.Xml.Serialization.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 <xref:System.Xml.XmlElement> items to process the data. If you specify a <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> value, the array contains only XML elements with that name. If you do not specify a <xref:System.Xml.Serialization.XmlAnyElementAttribute.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name>, or <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.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, <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.XmlAnyElementAttribute.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 <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name>, or <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> and <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> value set, the behavior during deserialization is unspecified. |
| 89 | +You can also apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a field that returns a single <xref:System.Xml.XmlElement> object. If you do so, you must use the properties and methods of the <xref:System.Xml.XmlElement> class to recursively iterate through the unknown elements. |
91 | 90 |
|
92 | | - You can also apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a field that returns a single <xref:System.Xml.XmlElement> object. If you do so, you must use the properties and methods of the <xref:System.Xml.XmlElement> class to recursively iterate through the unknown elements. |
| 91 | +You can apply multiple instances of the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a class member, but each instance must have a distinct <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property value. Or, if the same <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property is set for each instance, a distinct <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> property value must be set for each instance. |
93 | 92 |
|
94 | | - You can apply multiple instances of the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a class member, but each instance must have a distinct <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property value. Or, if the same <xref:System.Xml.Serialization.XmlAnyElementAttribute.Name> property is set for each instance, a distinct <xref:System.Xml.Serialization.XmlAnyElementAttribute.Namespace> property value must be set for each instance. |
95 | | -
|
96 | | - The <xref:System.Xml.Serialization.XmlSerializer.UnknownNode> and <xref:System.Xml.Serialization.XmlSerializer.UnknownAttribute> events of the <xref:System.Xml.Serialization.XmlSerializer> do not occur if you apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a member of a class. |
| 93 | +The <xref:System.Xml.Serialization.XmlSerializer.UnknownNode> and <xref:System.Xml.Serialization.XmlSerializer.UnknownAttribute> events of the <xref:System.Xml.Serialization.XmlSerializer> do not occur if you apply the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a member of a class. |
97 | 94 |
|
98 | 95 | > [!NOTE] |
99 | 96 | > You can use the word `XmlAnyElement` in your code instead of the longer <xref:System.Xml.Serialization.XmlAnyElementAttribute>. |
100 | 97 |
|
101 | | - For more information about using attributes, see [Attributes](/dotnet/standard/attributes/). |
102 | | -
|
103 | | -
|
| 98 | +For more information about using attributes, see [Attributes](/dotnet/standard/attributes/). |
104 | 99 |
|
105 | 100 | ## Examples |
106 | 101 | The following example applies the <xref:System.Xml.Serialization.XmlAnyElementAttribute> to a field named `AllElements` that returns an array of <xref:System.Xml.XmlElement> objects. |
|
0 commit comments