use*_*438 1 c# xml xpath nodes
我有一个XMLDocument查询的结果.
我想提取每个条目的<Property>值和适当的值<Notes>.
<?xml version="1.0"?>
<EADATA version="1.0" exporter="Enterprise Architect">
<Dataset_0>
<Data>
<Row>
<PropertyID>439</PropertyID>
<Object_ID>683</Object_ID>
<Property>tagged value</Property>
<ea_guid>{5BF3E019-277B-45c2-B2DE-1887A90C6944}</ea_guid>
</Row>
<Row>
<PropertyID>444</PropertyID>
<Object_ID>683</Object_ID>
<Property>Another Tagged value</Property>
<Notes>Another tagged value notes.</Notes>
<ea_guid>{42BE8BAA-06B8-4822-B79A-59F653C44453}</ea_guid>
</Row>
</Data>
</Dataset_0>
</EADATA>
Run Code Online (Sandbox Code Playgroud)
但是,如果<Notes>为空,则根本没有<Notes>标记.
XPath在这种情况下我应该写什么?
如果没有Notes元素,null,空字符串,您想要哪个值?
我会选择Row元素,SelectNodes然后检查Notes子元素是否存在并指定null(如下所示)或空字符串,如果不是:
foreach (XmlElement row in doc.SelectNodes("//Row"))
{
string prop = row.SelectSingleNode("Property").InnerText;
string notes = row.SelectSingleNode("Notes") != null ? row.SelectSingleNode("Notes").InnerText : null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
851 次 |
| 最近记录: |