使用 linq 从 xml 中读取特定元素
Read specific element from xml using linq
我正在尝试使用 c# google API 检索邮政编码。
如何为以下 xml 编写 linq 查询。
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>
如果您要获取邮政编码值,则:
var res = (from el in xmlElm.Descendants("address_component")
where el.Element("type").Value == "postal_code"
select el).FirstOrDefault();
我正在尝试使用 c# google API 检索邮政编码。 如何为以下 xml 编写 linq 查询。
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>
如果您要获取邮政编码值,则:
var res = (from el in xmlElm.Descendants("address_component")
where el.Element("type").Value == "postal_code"
select el).FirstOrDefault();