如何从子节点获取 C# 中的所有文本,但避免使用具有特定属性(例如已删除)的子节点中的文本

  • 本文关键字:文本 子节点 属性 删除 获取 c# 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>

法典:

textNodes = levelNode.SelectNodes("guidance");
string text = textNodes.InnerText;

我收到文本

"公司资本的金额 为PRU 9.3.30 R2006/43MIPRU 4.2.11 R2006/43的目的维护的资源"。

我想要的是,如果文本子节点内有一个撤销的属性,那么文本将不会包含在文本变量中。 因此,文本将在上面的示例中:

"公司资本的金额 为2006/43MIPRU 4.2.11 R目的维持的资源">

你可以这样做

var values = xmlDoc.DocumentElement
.SelectSingleNode("//guidance")
.SelectNodes("//*[not(@revoked)]/text()")
.OfType<XmlNode>()
.Select(n => n.Value.Trim());
string text = string.Concat(values);

命名空间

using System;
using System.Linq;
using System.Xml;

最新更新