如何反序列化这种类型的xml文件?

发布于 2024-12-29 10:33:56 字数 579 浏览 3 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亚希 2025-01-05 10:33:56

如果您确定缺少根只是 XML 的一个问题 - 只需手动添加它:

string fileContent = File.ReadAllText(path);
string rawXml = "<root>" + fileContent + "</root>";

// now you can use LINQ-to-XML or whatever
XDocument xdoc = XDocument.Load(rawXml);

If you sure that missing root is only the one issue with your XML - just add it manually:

string fileContent = File.ReadAllText(path);
string rawXml = "<root>" + fileContent + "</root>";

// now you can use LINQ-to-XML or whatever
XDocument xdoc = XDocument.Load(rawXml);
叶落知秋 2025-01-05 10:33:56

您还可以直接加载 XML 片段,通过

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

using (XmlReader reader = XmlReader.Create("tracelog.xml", settings))
{
    while (reader.Read())
    {
        // Process each node of the fragment,
        // possibly using reader.ReadSubtree()
    }
}

reader.ReadSubTree() 的结果传递给 XElement.Load(... )

You can also load an XML Fragment directly, via

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

using (XmlReader reader = XmlReader.Create("tracelog.xml", settings))
{
    while (reader.Read())
    {
        // Process each node of the fragment,
        // possibly using reader.ReadSubtree()
    }
}

You would create XElements by passing the results of reader.ReadSubTree() to XElement.Load(...).

垂暮老矣 2025-01-05 10:33:56

首先,它不是 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.

不顾 2025-01-05 10:33:56

如果它没有根目录,那么它就不是一个 XML 文件。如果您尝试解析它,解析器将抛出错误。您可以这样做

<?xml version="1.0"?>
<Root>
--- add your file content here
</Root>  

,然后将此文件路径提供给解析器。

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

<?xml version="1.0"?>
<Root>
--- add your file content here
</Root>  

then give this file path to the parser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文