简体   繁体   中英

Deserializing XML to Objects defined in multiple schemas

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>

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM