[英]How to get all texts in C# from child nodes but avoid a text from those child that has specific attribute (such as removed)
I have the following XML:我有以下 XML:
<guidance icon="G">
<text newpara="N">
The amount of a firm'scapital
resources maintained for the purposes of <k2:xref revoked="20061231" in_force_from="20050114">
<k2:xrefout revoked="20061231">PRU 9.3.30 R</k2:xrefout>
</k2:xref>
<changed-by in_force_from="20070101">2006/43</changed-by>
</text>
<text in_force_from="20070101" newpara="N">
<k2:xref in_force_from="20070101">
<k2:xrefout xlink:type="simple" xlink:label="SRC527" k2:destElementId="DES60" >MIPRU 4.2.11 R</k2:xrefout>
</k2:xref>
<changed-by in_force_from="20070101">2006/43</changed-by>
</text>
</guidance>
Code:代码:
textNodes = levelNode.SelectNodes("guidance");
string text = textNodes.InnerText;
I am getting the text我收到了文字
"The amount of a firm'scapital resources maintained for the purposes of PRU 9.3.30 R2006/43MIPRU 4.2.11 R2006/43".
“为 PRU 9.3.30 R2006/43MIPRU 4.2.11 R2006/43 的目的而维持的公司资本资源数量”。
What I want if there is a revoked attribute inside text child nodes, then the text will not be included in the text variable.如果文本子节点中有一个已撤销的属性,我想要什么,那么文本将不会包含在文本变量中。 So, the Text will be in the above example:
因此,文本将在上面的示例中:
"The amount of a firm'scapital resources maintained for the purposes of 2006/43MIPRU 4.2.11 R"
“为 2006/43MIPRU 4.2.11 R 的目的而维持的公司资本资源的数量”
You can do it like this你可以这样做
var values = xmlDoc.DocumentElement
.SelectSingleNode("//guidance")
.SelectNodes("//*[not(@revoked)]/text()")
.OfType<XmlNode>()
.Select(n => n.Value.Trim());
string text = string.Concat(values);
Namespaces命名空间
using System;
using System.Linq;
using System.Xml;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.