将 XML 反序列化为多个模式中定义的对象 [英] Deserializing XML to Objects defined in multiple schemas

查看:22
本文介绍了将 XML 反序列化为多个模式中定义的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,其中包含来自 2 个 XML 模式的类型.一个(他们的.xsd)是我正在与之集成(并且无法编辑)的专有模式.为此,我定义了自己的类型 (mine.xsd),即any"元素中的元素是专有类型.

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.

我使用 Visual Studio 的 xsd.exe 从架构生成 C# 类.但是,专有类型中的any"元素生成为 XmlElement[],因此我的类型不会被反序列化.

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.

所以我想我可以采用以下两种方法之一:要么生成将我的类型反序列化而不是将其保留为 XmlElement 的类,要么采用 XmlElements 并单独反序列化它们.要反序列化我需要一个 XmlReader,所以我需要从一个 XmlElement 转到一个 XmlReader,我不知道该怎么做.谢谢.

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.

示例:文件:他们的.xsd

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>

文件:mine.xsd

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>

文件:message.xml

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>

推荐答案

来自问题:

要反序列化我需要一个 XmlReader,所以我需要从一个 XmlElement 转到一个 XmlReader,我不知道该怎么做

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
}

这篇关于将 XML 反序列化为多个模式中定义的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆