如何反序列化这种类型的xml文件?
<row>
<id>1</id>
<code></code>
<name></name>
<address></address>
<state></state>
<zone>?</zone>
</row>
<row>
<id>2</id>
<code>AA</code>
<name>Ataria</name>
<address>Sitapur National Highway 24, Uttar Pradesh</address>
<state>Uttar Pradesh</state>
<zone>NER</zone>
</row>
我在这个 xml 文件中没有根元素,只有行元素开始和结束这个 xml 文件。 如何反序列化这种类型的数据?在 c# 中
<row>
<id>1</id>
<code></code>
<name></name>
<address></address>
<state></state>
<zone>?</zone>
</row>
<row>
<id>2</id>
<code>AA</code>
<name>Ataria</name>
<address>Sitapur National Highway 24, Uttar Pradesh</address>
<state>Uttar Pradesh</state>
<zone>NER</zone>
</row>
i have no root element in this xml file only row element start and end this xml file.
how Deserializing this type of data ? in c#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确定缺少根只是 XML 的一个问题 - 只需手动添加它:
If you sure that missing root is only the one issue with your XML - just add it manually:
您还可以直接加载 XML 片段,通过
将
reader.ReadSubTree()
的结果传递给XElement.Load(... )
。You can also load an XML Fragment directly, via
You would create
XElement
s by passing the results ofreader.ReadSubTree()
toXElement.Load(...)
.首先,它不是 XML 文件,或者至少它不代表 XML 文档。
一种选择是将文件复制到一个新文件中,该文件确实具有文档开始/结束标记...然后您可以将其作为普通文档加载。只需创建一个文件,写入文档开始标记,复制该文件的内容,然后写入文档结束标记,并关闭文件句柄即可。您甚至可以在内存中执行此操作。
或者,可能可以按片段的方式读取它 - 可能通过
XmlReader
。我不能说这是我所做的事情,而且我通常会鼓励您创建一个完整的 XML 文件,因为这样您就会进入更熟悉的领域。Well to start with, it's not an XML file - or at least, it doesn't represent an XML document.
One option would be to copy the file into a new file which does have document start/end tags... then you can load it as a normal document. Just create a file, write a document start tag, copy the contents of this file, then write a document end tag, and close the file handle. You could even do this in memory.
Alternatively, it may be possible to read it as it is, in fragments - possibly via
XmlReader
. I can't say it's something I've done, and I'd generally encourage you to create a full XML file instead, as then you'll be on more familiar territory.如果它没有根目录,那么它就不是一个 XML 文件。如果您尝试解析它,解析器将抛出错误。您可以这样做
,然后将此文件路径提供给解析器。
its not an XML file if it doesn't have the root. parser will throw an error if you try to parse it. you can do this way
then give this file path to the parser.