我们有这样的 XML:
<Example>
<Node>Some text here
<ChildNode>Child 1</ChildNode>
<ChildNode>Child 2</ChildNode>
</Node>
</Example>
我们正在使用 XmlDocument
来解析它。
当我们有“节点”元素的 XmlNode 时,XmlNode.InnerText
返回给我们:
"Some text hereChild 1Child2"
我们如何在没有子节点的内部文本的情况下获取 Node 元素的内部文本?我们真的不想使用任何 RegEx 或字符串拆分来完成此操作。
注意:我们也不想切换到使用一组不同的类来解析此 XML,这会导致代码更改太多。
最佳答案
var doc = new XmlDocument();
doc.LoadXml(xml);
var text = doc.SelectSingleNode("Example/Node/text()").InnerText; // or .Value
返回
"Some text here\r\n "
和text.Trim()
返回
"Some text here"
关于c# - XmlNode.InnerText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714962/