2011-08-19 27 views
0

请人帮助我我需要使用从我的应用程序返回xml的Web服务,下载xml的代码工作正常,但我需要提取值从xml文件,但我不断从代码获取空返回值,正是GetLocationFromXml()方法是返回null的方法,GetLocationAsXMLFromHost()方法正常工作。从xml访问后代元素在c中使用linq到xml返回null#

这是完整的类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using AMIS.Core.DTOs; 
using System.Net; 
using System.Xml.Linq; 
using System.Xml; 
using System.Linq; 

public class GeoLocationService 
{ 
    private string _hostWebSite = "http://api.hostip.info"; 
    private readonly XNamespace _hostNameSpace = "http://www.hostip.info/api"; 
    private readonly XNamespace _hostGmlNameSpace = "http://www.opengis.net/gml"; 

public LocationInfo GetLocationInfoFromIPAddress(string userHostIpAddress) 
{ 
    IPAddress ipAddress = null; 
    IPAddress.TryParse(userHostIpAddress, out ipAddress); 
    string xmlData = GetLocationAsXMLFromHost(ipAddress.ToString()); 
    LocationInfo locationInfo = GetLocationFromXml(xmlData); 
    return locationInfo; 
} 

private string GetLocationAsXMLFromHost(string userHostIpAddress) 
{ 
    WebClient webClient= new WebClient(); 
    string formattedUrl = string.Format(_hostWebSite + "/?ip={0}", userHostIpAddress); 
    var xmlData = webClient.DownloadString(formattedUrl); 
    return xmlData; 
} 

private LocationInfo GetLocationFromXml(string xmlData) 
{ 
    LocationInfo locationInfo = new LocationInfo(); 
    var xmlResponse = XDocument.Parse(xmlData); 
    var nameSpace = (XNamespace)_hostNameSpace; 
    var gmlNameSpace = (XNamespace)_hostGmlNameSpace; 

    try 
    { 
     locationInfo = (from x in xmlResponse.Descendants(nameSpace + "Hostip") 
         select new LocationInfo 
         { 
          CountryName = x.Element(nameSpace + "countryName").Value, 
          CountryAbbreviation = x.Element(nameSpace + "countryAbbrev").Value, 
          LocationInCountry = x.Element(gmlNameSpace + "name").Value 
         }).SingleOrDefault(); 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
    return locationInfo; 
} 
} 

和XML文件低于

<?xml version="1.0" encoding="iso-8859-1"?> 
<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>41.78.8.3</ip> 
    <gml:name>(Unknown city)</gml:name> 
    <countryName>NIGERIA</countryName> 
    <countryAbbrev>NG</countryAbbrev> 
    <!-- Co-ordinates are unavailable --> 
    </Hostip> 
</gml:featureMember> 
</HostipLookupResultSet> 
+0

什么是'_hostNameSpace'?如果您可以发布简短但完整的*程序,这将有所帮助。 –

+0

对不起,我省略了它 –

+0

对不起,我省略了私人字符串_hostWebSite =“http://api.hostip.info”; 私人字符串_hostNameSpace =“http://www.hostip.info/api”; 私人字符串_hostGmlNameSpace =“http://www.opengis.net/gml”; –

回答

1

鉴于评论,我怀疑可以是简单的问题,因为:

private string _hostNameSpace = "hostip.info/api"; 

应该是:

private string _hostNameSpace = "http://hostip.info/api"; 

(同上,用于其他人。)我个人会做那么的XNamespace值与启动:

private static readonly XNamespace HostNameSpace = "http://hostip.info/api"; 

编辑:好的,你的榜样乱搞后(这本来是短了很多并且更完整)我弄清楚了什么是错误的:您正在使用“主机命名空间”查找元素 - 但XML中的元素不在任何命名空间中。只是摆脱这些名称空间位,并且它工作正常。

+0

我已经尝试过它仍然没有运气 –

+0

@Adewole:编辑。基本上从您的代码中删除'_hostNameSpace +'位......您正在寻找名称空间中的元素,但它们不在该名称空间中...... –

+0

感谢它现在正常工作,非常感谢 –