C#反序列化包含多个根类型元素的Xml文件 [英] C# deserialize Xml file containing more than one root type element
本文介绍了C#反序列化包含多个根类型元素的Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C# - 我如何反序列化具有多种根元素的xml文件?
SampleFile.Xml以下示例包含在一个文件中:
C# - How may I deserialize an xml file the has more than one type of root elements?
SampleFile.Xml Example below all contained in one file:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
</book>
<book id="bk102">
<author>Jeanette, Dasha</author>
<title>Quack the Duck</title>
<genre>Fantasy</genre>
<price>5.95</price>
</book>
</catalog>
<ServerConfig loggingEnabled="1">
<Servers>
<Server host="test1.example.com" port="9999" />
<Server host="test2.example.com" port="8888" />
</Servers>
</ServerConfig>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NameList>Linguini</NameList>
<Item>MACBook</Item>
<Brand>Apple</Brand>
</Customer>
我''我做很多序列化/反序列化但没有多个类型的根元素。我会感谢你的一些帮助。谢谢!
I''ve done a lot of serialize / deserializing but not with more than one type root element. I''d appreciate some help on this. Thanks ahead!
推荐答案
如果您将两个XML代码块合并到一个文件中,或者即使您的第二个代码块是一个文件,它也会创建一个文件没有格式良好的XML。真正的XML只有一个根。从这个意义上说,你的文件不是XML。
-SA
If you merge the two XML code blocks in one file, or even if your second code block is one single file, it make a file which is not well-formed XML. A real XML has only one root. In this sense, your file ix not XML.
—SA
谢谢谢谢谢谢。你的回答正是我的想法。
我想我必须提取每个单独的根元素组,并进行反序列化。
再次,谢谢!
Thank you, Sergey. Your answer is precisely what I thought.
I think I will have to extract each individual root element group, and process deserialize it.
Again, thanks!
这篇关于C#反序列化包含多个根类型元素的Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文