2

所以我有这个 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包含空TenChucVu标签的标签,所以结果是这样的:

<ChucVu>
  <MaChucVu>1
  </MaChucVu>
  <TenChucVu>
  </TenChucVu>
</ChucVu>
<ChucVu>
  <MaChucVu>2
  </MaChucVu>
  <TenChucVu>
  </TenChucVu>
</ChucVu>
4

4 回答 4

3
XDocument doc = ...;
var query = doc.XPathSelectElements("//ChucVu[TenChucVu='']");
于 2012-08-13T13:32:10.757 回答
2

另一个应该工作的 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
于 2012-08-13T13:46:53.603 回答
2

您需要的 XPath 是:

/*/ChucVu[not(string(TenChucVu))]

或者

/*/ChucVu[string-length(TenChucVu)=0]
于 2012-08-13T13:32:09.310 回答
0

你可以这样:

  string xparth = String.Format("//ChucVu[TenChucVu='{0}']", '');
            XmlDocument doc = new XmlDocument();
            doc.Load("Xml");
            XmlElement root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode(xparth);
            XmlNodeList list = root.SelectNodes(xparth);
于 2012-08-13T13:51:39.877 回答