如何获得,有一个元素:它叫什么名字?叫什么名字、如何获得、有一个、元素

由网友(红唇爱上烟)分享简介:我需要从该XML的国家或地区名称: http://api.hostip.info /?ip=12.215.42.19 I need to get the CountryName from this XML: http://api.hostip.info/?ip=12.215.42.19响应XML是:我需要从该XML的国家或地区名称: http://api.hostip.info /?ip=12.215.42.19

I need to get the CountryName from this XML: 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>

问题是我不能包括后代的方法,因为它抛出:

Problem is I can't include : in the Descendants method because it throws:

XmlException:在':'chracater,   十六进制值0x3A的不能   包括在一个名称

XmlException: The ':' chracater, hexadecimal value 0x3A, cannot be included in a name.

感谢

推荐答案

试试这个

var descendants = from i in XDocument.Load(xml).Descendants("Hostip")
select i.Element("countryName");

更新

完成code下载XML并发现国家名称的名字

complete code for downloading the xml and finding the name of 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");
阅读全文

相关推荐

最新文章