简体   繁体   中英

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:

<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".

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"

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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