XML 解析器,多个根 [英] XML parser, multiple roots
问题描述
这是输入字符串的一部分,我无法修改它,它总是以这种方式出现(通过共享内存),但是我当然可以在将其放入字符串后进行修改:
SCPUCLK 2930 ;SCPUMUL</id><label>CPU乘数</label><value>11.0</value></sys><sys><id>SCPUFSB</id><label>CPU FSB<;/label>266
我都读过它:
String.Concat(XElement.Parse(encoding.GetString(bytes)).后代(价值").Select(v => v.Value));
和:
XmlDocument 文档 = new XmlDocument();document.LoadXml(encoding.GetString(bytes));XmlNode node = document.DocumentElement.SelectSingleNode("//value");Console.WriteLine("node = " + node);
但是它们在运行时都出现错误;输入有多个根(有多个根元素引用),我不想分割字符串.
他们有什么方法可以读取字符串,在
和 之间取值而不将字符串分成多个输入?
这不是一个格式良好的 XML 文档,因此大多数 XML 工具将无法处理它.
一个例外是 XmlReader
.查找 XmlReaderSettings.ConformanceLevel
在 MSDN 中.如果将其设置为 ConformanceLevel.Fragment
,则可以使用这些设置创建 XmlReader
并使用它从没有顶级元素的流中读取元素.>
您必须编写使用 XmlReader.Read()
的代码来执行此操作 - 您不能仅将其提供给 XmlDocument
(这确实需要单个顶级元素).
例如
var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };使用 (var reader = XmlReader.Create(stream, readerSettings)){而 (reader.Read()){使用 (var fragmentReader = reader.ReadSubtree()){如果 (fragmentReader.Read()){var fragment = XNode.ReadFrom(fragmentReader) as XElement;//用片段做一些事情}}}}
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?
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
}
}
}
}
这篇关于XML 解析器,多个根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!