繁体   English   中英

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

[英]How to get all texts in C# from child nodes but avoid a text from those child that has specific attribute (such as removed)

我有以下 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM