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>
0

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

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?
It should be fine - perhaps add a MoveToContent()?
Nevermind, I figured out the problem, after adding your suggestion I was passing the wrong type to the serializer. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.