将 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
}