XmlNode.InnerText [英] XmlNode.InnerText

查看:41
本文介绍了XmlNode.InnerText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有这样的XML:

<Example>
  <Node>Some text here
    <ChildNode>Child 1</ChildNode>
    <ChildNode>Child 2</ChildNode>
  </Node>
</Example>

我们正在使用 XmlDocument 对此进行解析.

We are using XmlDocument to parse this.

当我们拥有"Node"元素的XmlNode时, XmlNode.InnerText 会向我们返回此信息:

When we have the XmlNode of the "Node" element, XmlNode.InnerText returns us this:

"Some text hereChild 1Child2"

如何在没有子节点内部文本的情况下获取 Node 元素的内部文本?我们真的不想使用任何RegEx或字符串拆分来完成此操作.

How can we get the inner text of the Node element without the child nodes' inner text? We don't really want to use any RegEx or string splitting to accomplish this.

注意:我们也不想切换到使用一组不同的类来解析此XML,这将是太多的代码更改.

Note: We also don't want to switch to using a different set of classes to parse this XML, it would be too much of a code change.

推荐答案

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"

这篇关于XmlNode.InnerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆