2011-06-20 53 views
2

我正在嘗試使用WebService獲取荷蘭地點的當前天氣。XML - 標題包含非法字符

 public void GetWeather() 
    { 
     net.webservice.GlobalWeather.GlobalWeather GlobalWeatherService = new net.webservice.GlobalWeather.GlobalWeather(); 
     string SoapResult = GlobalWeatherService.GetWeather(Location, "Netherlands"); 

     XmlDocument XmlDoc = new XmlDocument(); 
     XmlDoc.Load(SoapResult); 
     XmlNodeList XmlForecast = XmlDoc.GetElementsByTagName("CurrentWeather"); 
    } 

WebService的回發一個XML文件作爲一個字符串

<?xml version="1.0" encoding="utf-16"?> 
<CurrentWeather> 
<Location>Amsterdam Airport Schiphol, Netherlands (EHAM) 52-18N 004-46E -2M</Location> 
<Time>Jun 20, 2011 - 06:25 AM EDT/2011.06.20 1025 UTC</Time> 
<Wind> from the SW (220 degrees) at 9 MPH (8 KT):0</Wind> 
<Visibility> greater than 7 mile(s):0</Visibility> 
<SkyConditions> partly cloudy</SkyConditions> 
<Temperature> 62 F (17 C)</Temperature> 
<DewPoint> 51 F (11 C)</DewPoint> 
<RelativeHumidity> 67%</RelativeHumidity> 
<Pressure> 29.88 in. Hg (1012 hPa)</Pressure> 
<Status>Success</Status> 
</CurrentWeather> 

但是,當我嘗試在XmlDocument的我得到XmlDoc.Load的ArguementException(路徑中具有非法字符)來加載結果(SoapResult );

我在做什麼錯?

回答

3

XMLDocument.load方法()來加載此GlobalWeatherService.GetWeather(Location, "Netherlands")

,加載XML文檔從指定的URL中獲取包含要加載的XML文檔的文件。該URL可以是本地文件或HTTP URL(Web地址)。

如果文件名是零長度字符串,僅包含空格或包含一個或多個無效字符,則會引發ArgumentException。

改爲使用LoadXml()。

1

XmlDoc.Load需要一個文件路徑作爲輸入參數,而不是實際的字符串

也許你需要作爲Filburt建議使用XmlDoc.LoadXml

+2

[XmlDocument.LoadXml(SoapResult)](http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx) – Filburt

+0

@Filburt感謝您指出了這一點! – V4Vendetta

+0

啊謝謝Filburt! –