2011-08-19 44 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

感謝它現在正常工作,非常感謝 –