Any Element Binding SupportĀ
The .NET Framework provides partial binding support for the <any> element.
When generating source code from an XML Schema document, Xsd.exe translates the <any> element into a field of type System.Xml.XmlElement, with an attribute XmlAnyElementAttribute.
Explanation
The <any> wildcard allows any element (an element of any name or type) to appear in the given position in an XML instance document, with some constraints.
When generating source code from an XML Schema document, Xsd.exe translates the <any> element into a field of type System.Xml.XmlElement, with an attribute XmlAnyElementAttribute. This attribute allows a class to represent arbitrary XML elements without binding them to non-XML types identified by other possible fields or properties of the class.
If the <any> element appears with a maxOccurs attribute having a value greater than 1 or unbounded
, Xsd.exe produces an XmlElement array with an XmlAnyElement attribute. A single XmlElement is produced when the value of the maxOccurs attribute is 1
(the default) or 0
. For an explanation of this behavior, see the maxOccurs attribute.
When generating an XML Schema document from a set of classes in an assembly, Xsd.exe performs the reverse translation: a field or property of type System.Xml.XmlElement, with an attribute XmlAnyElementAttribute, is translated into an <any> element.
namespace and processContents Attributes
During generation of XML Schema definitions or classes, the namespace and processContents attributes are ignored. This means that, whatever those attribute values are in an original XML Schema definition, when Xsd.exe generates classes from that definition and then generates another XML schema from those classes, the generated XML schema's <any> element no longer has those attributes. Therefore, the latter schema reverts to the following default values:
namespace="##any"
: The namespace attribute specifies the namespaces in which the element must be defined. A value of##any
allows any namespace.processContents="strict"
: When validating an instance XML document for conformance to its specified XML schema, make sure that all element types are validated according to the specified namespace(s). If an element type is not recognized, validation fails.
Likewise, during deserialization from XML documents to objects, the namespace and processContents attributes are ignored. Instead, the XmlSerializer class behaves as though the two attributes were fixed to the following values:
namespace="##any"
: This is the default value.processContents="lax"
: When validating an instance XML document for conformance to its specified XML schema, validate the element only if its namespace can be obtained; otherwise, make sure that an unrecognized element consists of well-formed XML.
Note
Even though at run time the .NET Framework assumes processContents="lax"
, at deployment time the .NET Framework generates XML Schema definitions where the default processContents="strict"
is implicitly specified.
Xsd.exe and the XmlSerializer class provide the same bindings for the namespace and processContents attributes when they appear in the <anyAttribute> element.
Example
Input XML Schema document:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
<xsd:element name="complexInstance" type="MyComplexType"/>
<xsd:complexType name="MyComplexType">
<xsd:sequence>
<xsd:element name="field1" type="xsd:string"/>
<xsd:any namespace="##local" processContents="strict"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
C# class generated from the preceding XML Schema document:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("complexInstance", Namespace="http://example.org/", IsNullable=false)]
public class MyComplexType {
public string field1;
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement Any;
}
XML Schema complex type generated from an assembly compiled from the preceding C# source:
<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="complexInstance" type="tns:MyComplexType" />
<xs:complexType name="MyComplexType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="field1" type="xs:string" />
<xs:any minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Possible Attributes | Binding Support |
---|---|
id |
The Xsd.exe utility ignores the id attribute, which is intended to provide a unique identifier. |
maxOccurs |
See the Explanation section in this topic. Also see the MaxOccurs Attribute Binding Support attribute. |
minOccurs |
When generating source code from an XML Schema document, the Xsd.exe tool ignores the minOccurs attribute when applied to the <any> element. When generating an XSD document from classes, the Xsd.exe tool produces a value of See the MinOccurs Attribute Binding Support attribute. |
namespace |
See the section namespace and processContents Attributes within this topic. |
processContents |
See the section namespace and processContents Attributes within this topic. |
Possible parent elements: <choice>, <sequence>
Possible child elements: <annotation>