2011-09-05 49 views
0
<?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
     <soap:Body> 
     <TimNhanh_Text_ListNews_OfflineResponse xmlns="http://tempuri.org/"> 
      <TimNhanh_Text_ListNews_OfflineResult> 
      <string>string</string> 
      <string>string</string> 
      </TimNhanh_Text_ListNews_OfflineResult> 
     </TimNhanh_Text_ListNews_OfflineResponse> 
     </soap:Body> 
    </soap:Envelope> 

如何在xml中获取节点?如何获取XML元素(<Soap:body>)

我使用:

XDocument dataDoc = XDocument.Load(new StringReader(result)); 
foreach (var word in dataDoc.Descendants("TimNhanh_Text_ListNews_OfflineResult")) 
{ 

} 

但这不活跃。

回答

0
private void wc1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     try 
     { 
      //http://xml.weather.yahoo.com/forecastrss?p=NOXX0035&u=c 

      XDocument doc = new XDocument(); 
      doc = XDocument.Parse(e.Result); 


      bool forecastTomorrow = true; 

      foreach (XElement element in doc.Descendants("channel").First().Descendants("item")) 
      { 

       if (element.ToString().Contains("Current Conditions")) 
       { 
        ContentGrid.Visibility = Visibility.Visible; 

        string conditions = (string)element.Element("description"); 



        string temp = element.Attribute("temp").Value; 


        Uri uri = new Uri("Images/" + imageName + ".png", UriKind.Relative); 
        ImageSource img = new BitmapImage(uri); 
        imgWeather1.Source = img; 

        lblCurrent_conditions.Text = HttpUtility.HtmlDecode(arrayConditions[0]) +"\n" + arrayConditions[1] + "\n" + arrayConditions[2] + "\n" + arrayConditions[3] + "\n" + arrayConditions[4]; 
       } 
       else if (element.ToString().Contains("<yweather:forecast")) 
       { 
        ; 
        string day = element.Attribute("day").Value; 
        string conditions = element.Attribute("text").Value; 
        string tempLow = element.Attribute("low").Value; 
        string tempHigh = element.Attribute("high").Value; 
        string code = element.Attribute("code").Value; 

        int weatherCode = Convert.ToInt16(code); 
        string imageName = weatherImage(weatherCode); 

        Uri uri = new Uri("Images/" + imageName + ".png", UriKind.Relative); 
        ImageSource img = new BitmapImage(uri); 


       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
0

你要找的节点是在http://tempuri.org/命名空间,所以你需要考虑的是还有:

XDocument dataDoc = XDocument.Load(new StringReader(result)); 
foreach (var word in dataDoc.Descendants(
    XName.Get("TimNhanh_Text_ListNews_OfflineResult", "http://tempuri.org/")) 
{ 
    // continue here 
} 
+0

感谢carlosfigueira!我想要元素并添加到列表 beaucause我需要数据在节点字符串?你可以帮助我 ? – thongaduka