XML 解析器,多根

发布于 2024-12-01 18:06:00 字数 1032 浏览 0 评论 0原文

这是输入字符串的一部分,我无法修改它,它总是以这种方式出现(通过共享内存),但我当然可以在将其放入字符串后进行修改:

<sys><id>SCPUCLK</id><label>CPU Clock</label><value>2930</value></sys><sys><id>SCPUMUL</id><label>CPU Multiplier</label><value>11.0</value></sys><sys><id>SCPUFSB</id><label>CPU FSB</label><value>266</value></sys>

我已经用两者读取了它:

        String.Concat(
            XElement.Parse(encoding.GetString(bytes))
                .Descendants("value")
                .Select(v => v.Value));

和:

    XmlDocument document = new XmlDocument();
    document.LoadXml(encoding.GetString(bytes));
    XmlNode node = document.DocumentElement.SelectSingleNode("//value");
    Console.WriteLine("node = " + node);

但运行时都会出错;输入有多个根(有多个根元素引用),我不想分割字符串。

他们是否有任何方式读取字符串,获取 之间的值,而不将字符串吐入多个输入?

This is part of the input string, i can't modify it, it will always come in this way(via shared memory), but i can modify after i have put it into a string of course:

<sys><id>SCPUCLK</id><label>CPU Clock</label><value>2930</value></sys><sys><id>SCPUMUL</id><label>CPU Multiplier</label><value>11.0</value></sys><sys><id>SCPUFSB</id><label>CPU FSB</label><value>266</value></sys>

i've read it with both:

        String.Concat(
            XElement.Parse(encoding.GetString(bytes))
                .Descendants("value")
                .Select(v => v.Value));

and:

    XmlDocument document = new XmlDocument();
    document.LoadXml(encoding.GetString(bytes));
    XmlNode node = document.DocumentElement.SelectSingleNode("//value");
    Console.WriteLine("node = " + node);

but they both have an error when run; that the input has multiple roots(There are multiple root elements quote), i don't want to have to split the string.

Is their any way to read the string take the value between <value> and </value> without spiting the string into multiple inputs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

哑剧 2024-12-08 18:06:00

这不是格式良好的 XML 文档,因此大多数 XML 工具无法处理它。

XmlReader 是一个例外。查找 XmlReaderSettings.ConformanceLevel< /a> 在 MSDN 中。如果将其设置为 ConformanceLevel.Fragment,则可以使用这些设置创建一个 XmlReader 并使用它从没有顶级元素的流中读取元素。

您必须编写使用 XmlReader.Read() 的代码来执行此操作 - 您不能只将其提供给 XmlDocument (这确实要求有一个顶部级元素)。

例如,

var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stream, readerSettings))
{
    while (reader.Read())
    {
        using (var fragmentReader = reader.ReadSubtree())
        {
            if (fragmentReader.Read())
            {
                var fragment = XNode.ReadFrom(fragmentReader) as XElement;

                // do something with fragment
            }
        }
    }
}

That's not a well-formed XML document, so most XML tools won't be able to process it.

An exception is the XmlReader. Look up XmlReaderSettings.ConformanceLevel in MSDN. If you set it to ConformanceLevel.Fragment, you can create an XmlReader with those settings and use it to read elements from a stream that has no top-level element.

You have to write code that uses XmlReader.Read() to do this - you can't just feed it to an XmlDocument (which does require that there be a single top-level element).

e.g.,

var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stream, readerSettings))
{
    while (reader.Read())
    {
        using (var fragmentReader = reader.ReadSubtree())
        {
            if (fragmentReader.Read())
            {
                var fragment = XNode.ReadFrom(fragmentReader) as XElement;

                // do something with fragment
            }
        }
    }
}
扛刀软妹 2024-12-08 18:06:00

XML 元素必须有一个根元素,可以具有您想要的任何子结构。
您的 xml 字符串如下所示:

<sys>
   ...
</sys>
<sys>
   ...
</sys>

有效版本为:

<someRootElement>
  <sys>
   ...
  </sys>
  <sys>
   ...
  </sys>
</someElement>

尝试:

XmlDocument document = new XmlDocument();
document.LoadXml("<root>"+encoding.GetString(bytes)+"</root>");
XmlNode node = document.DocumentElement.SelectSingleNode("//value");
Console.WriteLine("node = " + node);

XML elements must have ONE root element, with whatever child structure you want.
Your xml string looks like:

<sys>
   ...
</sys>
<sys>
   ...
</sys>

The valid version would be:

<someRootElement>
  <sys>
   ...
  </sys>
  <sys>
   ...
  </sys>
</someElement>

Try:

XmlDocument document = new XmlDocument();
document.LoadXml("<root>"+encoding.GetString(bytes)+"</root>");
XmlNode node = document.DocumentElement.SelectSingleNode("//value");
Console.WriteLine("node = " + node);
缺⑴份安定 2024-12-08 18:06:00

此解决方案成功解析所有节点类型,包括文本节点:

var settings = new XmlReaderSettings{ConformanceLevel = ConformanceLevel.Fragment};
var reader = XmlReader.Create(stream, settings);
while(reader.Read())
{
    while(reader.NodeType != XmlNodeType.None)
    {
        if(reader.NodeType == XmlNodeType.XmlDeclaration)
        {
            reader.Skip();
            continue;
        }
        XNode node = XNode.ReadFrom(reader);
    }
}

跳过 XML 声明,因为它不是节点。

This solution parses successfully all node types, including text nodes:

var settings = new XmlReaderSettings{ConformanceLevel = ConformanceLevel.Fragment};
var reader = XmlReader.Create(stream, settings);
while(reader.Read())
{
    while(reader.NodeType != XmlNodeType.None)
    {
        if(reader.NodeType == XmlNodeType.XmlDeclaration)
        {
            reader.Skip();
            continue;
        }
        XNode node = XNode.ReadFrom(reader);
    }
}

Skips the XML declaration because it isn't a node.

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