我的XML如下所示:
<status_log>
<log>
<status>CREATED</status>
<detail></detail>
<datetime>03/17/2015 10:06:02</datetime>
</log>
<log>
<status>AC_VALIDATION</status>
<detail>Credit Transaction Response: 0 - , Error: -</detail>
<datetime>03/17/2015 10:06:04</datetime>
</log>
<log>
<status>CREATED</status>
<detail>Debit Transaction Response: 0 - , Error: -</detail>
<datetime>03/17/2015 10:06:05</datetime>
</log>
<log>
<status>INVALID</status>
<detail>Request ID: 110895, Transaction Number: A92C18E9 ,Status: 9, Error Code: 0</detail>
<datetime>03/18/2015 20:14:34</datetime>
</log>
<log>
<status>UPDATED</status>
<detail></detail>
<datetime>03/19/2015 06:06:03</datetime>
</log>
<log>
<status>AC_VALIDATION</status>
<detail>Credit Transaction Response: 0 - , Error: -</detail>
<datetime>03/19/2015 06:06:05</datetime>
</log>
<log>
<status>CREATED</status>
<detail>Debit Transaction Response: 0 - , Error: -</detail>
<datetime>03/19/2015 06:06:05</datetime>
</log>
<log>
<status>UPDATED</status>
<detail></detail>
<datetime>03/19/2015 06:06:12</datetime>
</log>
</status_log>
我想仅为在<log>
子元素中具有信用交易响应的元素获取<detail>
xml。
答案 0 :(得分:0)
您只需要Where扩展方法来过滤: -
var result = xdoc.Descendants("log")
.Where(x => (string)x.Element("detail") == "Credit Transaction Response");
<强>更新强>
如果您只想按Credit Transaction Response
过滤,而不是按完整文字进行过滤,则可以使用StartsWith
,如下所示: -
var result = xdoc.Descendants("log")
.Where(x => (string)x.Element("detail") != null
? x.Element("detail").Value.ToString().StartsWith("Credit Transaction Response")
: false);