如何获取 XmlNode 内的文本
如何获取 XmlNode 内的文本?见下文:
XmlNodeList nodes = rootNode.SelectNodes("descendant::*");
for (int i = 0; i < nodes.Count; i++)
{
XmlNode node = nodes.Item(i);
//TODO: Display only the text of only this node,
// not a concatenation of the text in all child nodes provided by InnerText
}
我最终想要做的是将“HELP:”添加到每个节点中的文本中。
How do I get the text that is within an XmlNode? See below:
XmlNodeList nodes = rootNode.SelectNodes("descendant::*");
for (int i = 0; i < nodes.Count; i++)
{
XmlNode node = nodes.Item(i);
//TODO: Display only the text of only this node,
// not a concatenation of the text in all child nodes provided by InnerText
}
And what I ultimately want to do is preppend "HELP: " to the text in each node.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法可能是迭代节点的所有直接子节点(使用
ChildNodes
)并测试每个节点的NodeType
以查看它是否为Text< /code> 或
CDATA
。不要忘记可能有多个文本节点。(仅供参考,如果您可以使用 .NET 3.5,那么 LINQ to XML 会更好用。)
The simplest way would probably be to iterate over all the direct children of the node (using
ChildNodes
) and test theNodeType
of each one to see if it'sText
orCDATA
. Don't forget that there may be multiple text nodes.(Just as an FYI, if you can use .NET 3.5, LINQ to XML is a lot nicer to use.)
在节点的子节点中搜索
NodeType
为Text
的节点,并使用该节点的Value
属性。请注意,您还可以通过使用
text()
节点类型测试来选择带有 XPath 的文本节点。Search the node's children for a node with
NodeType
ofText
, and use theValue
property of that node.Note that you can also select text nodes with XPath by using the
text()
node-type test.您可以读取 xmlnode 的 InnerText 属性
读取node.InnerText
you can read the InnerText property of xmlnode
read
node.InnerText
检查这一点
,您还可以检查当您编写“阅读器”时会得到哪些选项。
xml 文件
和阅读器非常基本但速度很快
Check this
also you might check what options you get when you write "reader."
xml file
and reader really basic but fast