views:

389

answers:

1

I have an XML document containing types from 2 XML schemas. One (theirs.xsd) is a proprietary schema that I am integrating with (and cannot edit). To do this I am defining my own type (mine.xsd) that is an element within an 'any' element is the proprietary type.

I use Visual Studio's xsd.exe to generate C# classes from the schemas. However, the 'any' element in the proprietary type is generated as XmlElement[], and therefore my type doesn't get deserialized.

So I guess I can go one of two ways: either generate classes that will deserialize my type rather then keeping it as an XmlElement, or take the XmlElements and deserialize them individually. To deserialize I need an XmlReader, so I would need to go from an XmlElement to an XmlReader which I'm not sure how to do. Thanks.

Example: File: theirs.xsd

<xs:element name="ProprietaryContainer">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

File: mine.xsd

<xs:element name="MyPairType">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="key" type="xs:string"/>
      <xs:element name="value" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

File: message.xml

<their:ProprietaryContainer>
  <their:name>pairContainer</their:name>
  <mine:MyPairType>
    <mine:key>abc</mine:key>
    <mine:value>long</mine:value>
  </mine:MyPairType>
</their:ProprietaryContainer>
A: 

From the question:

To deserialize I need an XmlReader, so I would need to go from an XmlElement to an XmlReader which I'm not sure how to do

using(XmlReader reader = new XmlNodeReader(element)) {
    //... use reader
}
Marc Gravell
Thanks, I'm trying this now, but I keep receiving an exception saying 'There is an error in the XML document'. Isn't an XmlElement valid for deserialization?
Partridge
It should be fine - perhaps add a MoveToContent()?
Marc Gravell
Nevermind, I figured out the problem, after adding your suggestion I was passing the wrong type to the serializer. Thanks for the help!
Partridge