所以我得到了这个XML:

<?xml version="1.0" encoding="utf-8"?>
<M_ChucVu>
  <ChucVu>
    <MaChucVu>1
    </MaChucVu>
    <TenChucVu>
    </TenChucVu>
  </ChucVu>
  <ChucVu>
    <MaChucVu>2
    </MaChucVu>
    <TenChucVu>
    </TenChucVu>
  </ChucVu>
  <ChucVu>
    <MaChucVu>23</MaChucVu>
    <TenChucVu>12</TenChucVu>
  </ChucVu>
  <ChucVu>
    <MaChucVu>44</MaChucVu>
    <TenChucVu>44</TenChucVu>
  </ChucVu>
</M_ChucVu>

我想检索包含空标记的ChucVu标记,结果是:
<ChucVu>
  <MaChucVu>1
  </MaChucVu>
  <TenChucVu>
  </TenChucVu>
</ChucVu>
<ChucVu>
  <MaChucVu>2
  </MaChucVu>
  <TenChucVu>
  </TenChucVu>
</ChucVu>

最佳答案

另一个有效的xpath

/M_ChucVu[./ChucVu/TenChucVu='']

例如
        var doc = new XmlDocument();
        doc.LoadXml(yourXmlString);
        var elem = doc.DocumentElement;
        var sel = elem.SelectNodes("/M_ChucVu[./ChucVu/TenChucVu!='']");
        // print or use sel.InnerXml

关于c# - 在C#中通过XPath获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11935196/

10-13 01:58