2011-02-11 148 views
5

我需要从这个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> 

问题是我不能包括:Descendants方法,因为它会抛出:

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

感谢

+0

顺便说一下你的标题和问题不匹配。你实际上并不需要名称中的元素:歪曲你对命名空间解决方案的回答 –

回答

5

试试这个

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

更新

下载XML并找到国家名称

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"); 
+0

我想出了一个最大的解决方案,我不得不引用命名空间,但是你更简单,不需要引用ns。谢谢 – emzero

+0

XDocument.Load()将获取一个URL,下载它并解析它,保存几行文字。 –

+0

很高兴帮助! –

1

您需要引用GML命名空间;一旦你这样做,你应该能够使用该出现的正确的标签名称导航“GML:”

UPDATE

我不知道你申请这什么情况,但这里有一个样本控制台应用程序的作品:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Linq; 

namespace LinqToXmlSample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      XElement x = XElement.Load("http://api.hostip.info/?ip=12.215.42.19"); 
      foreach (XElement hostip in x.Descendants("Hostip")) 
      { 
       string country = Convert.ToString(hostip.Element("countryName").Value); 
       Console.WriteLine(country); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
+0

我该如何引用它? – emzero

3

如何在LINQ应用命名空间XML的一个小例子名称完整代码:

XElement doc = XElement.Load("test.xml"); 
XNamespace ns = "http://www.opengis.net/gml"; 

var firstName = doc.Descendants(ns + "name").First().Value; 
+0

我真的不需要遍历我的问题中的“名称空间”元素。但+1如何应用LINQ to XML中的命名空间的记录=) – emzero

0
var gml = (XNamespace)"http://www.opengis.net/gml"; 
var doc = XDocument.Load(...) or XDocument.Parse(...); 
var name = doc.Descendants(gml + "featureMember").Descendants("countryName").First().Value; 

或者你可以去蛮力和去除所有命名空间:

void RemoveNamespace(XDocument xdoc) 
{ 
    foreach (XElement e in xdoc.Root.DescendantsAndSelf()) 
    { 
     if (e.Name.Namespace != XNamespace.None) 
     { 
      e.Name = XNamespace.None.GetName(e.Name.LocalName); 
     } 

     if (e.Attributes().Any(a => a.IsNamespaceDeclaration || a.Name.Namespace != XNamespace.None)) 
     { 
      e.ReplaceAttributes(e.Attributes().Select(a => a.IsNamespaceDeclaration ? null : a.Name.Namespace != XNamespace.None ? new XAttribute(XNamespace.None.GetName(a.Name.LocalName), a.Value) : a)); 
     } 
    } 
} 
+0

我是从内存中完成这项工作的,可能有错误 –

+0

您是对的。没有'XNamespace'的构造函数。使用'XNamespace.Get()'或将字符串转换为一个。 ;) –

+0

谢谢...我会更新 –