I am using xsd2code in order to create C# classes which can serialize/deserialize XML files based on a set of XSD files.
One XSD file contains an <any>
element:
<xs:complexType name="AnyPlugInContainerType">
<xs:sequence>
<xs:any namespace="http://www.extra-standard.de/namespace/plugins/1" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
The XSD creator expects that elements based on the following abstract type are used to fill the any
sequence:
<xs:complexType name="AbstractPlugInType" abstract="true"/>
The generated C# code of the first XSD section reads:
[XmlAnyElementAttribute(Namespace = "http://www.extra-standard.de/namespace/plugins/1", Order = 0)]
public List<System.Xml.XmlElement> Any
{
get { return _any; }
set { _any = value; }
}
When populating an object in C# which should be serialized later to an XML file,
how can I add items to the Any
list?
In order to create an XmlElement, I think I need an XmlDocument first. Afterwards, XmlDocument.CreateElement
can be called. How do I get the XmlDocument?
When trying to change the type of the elements in the Any
list from XmlElement to AbstractPlugInType
, the following run-time error is raised when serializing the object:
XmlAnyElement can only be used with classes of type XmlNode or a type deriving from XmlNode
Do I need to manually replace the XmlAnyElementAttribute
attribute with XmlArrayItemAttribute
leading to:
[XmlArrayItemAttribute("AnyPlugInContainerType"Namespace = "http://www.extra-standard.de/namespace/plugins/1", Order = 0)]
public List<AbstractPlugInType> Any
{
get { return _any; }
set { _any = value; }
}
Any help appreciated!
(An old question on a similar problem exists which has been unanswered so far: Xsd2Code - Using <any> element to join schemas)