簡體   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