如何获得一个具有以下名称的元素?

emz*_*ero 5 .net c# xml linq linq-to-xml

我需要从这个XML获取CountryName:http://api.hostip.info/?ip = 12.215.42.19

响应XML是:

<HostipLookupResultSet version="1.0.1"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">

  <gml:description>This is the Hostip Lookup
  Service</gml:description>
  <gml:name>hostip</gml:name>
  <gml:boundedBy>
    <gml:Null>inapplicable</gml:Null>
  </gml:boundedBy>
  <gml:featureMember>
    <Hostip>
      <ip>12.215.42.19</ip>
      <gml:name>Sugar Grove, IL</gml:name>
      <countryName>UNITED STATES</countryName>
      <countryAbbrev>US</countryAbbrev>
      <!-- Co-ordinates are available as lng,lat -->
      <ipLocation>
        <gml:pointProperty>
          <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">

            <gml:coordinates>-88.4588,41.7696</gml:coordinates>
          </gml:Point>
        </gml:pointProperty>
      </ipLocation>
    </Hostip>
  </gml:featureMember>
</HostipLookupResultSet>
Run Code Online (Sandbox Code Playgroud)

问题是我不能:Descendants方法中包含因为它抛出:

XmlException:':'chracater,十六进制值0x3A,不能包含在名称中.

谢谢

san*_*ngh 5

试试这个

var descendants = from i in XDocument.Load(xml).Descendants("Hostip")
select i.Element("countryName");
Run Code Online (Sandbox Code Playgroud)

更新

下载xml并找到countryName名称的完整代码

string xml;
using(var web = new WebClient())
{
xml = web.DownloadString("http://api.hostip.info/?ip=12.215.42.19");
}
var descendants = from i in XDocument.Parse(xml).Descendants("Hostip")
select i.Element("countryName");
Run Code Online (Sandbox Code Playgroud)