如何获得,有一个元素:它叫什么名字? [英] How to get an element that has : in its name?

查看:141
本文介绍了如何获得,有一个元素:它叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从该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");

这篇关于如何获得,有一个元素:它叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆