如何获取XML元素(<soap:body>)</soap:body>

时间:2011-09-05 03:46:50

标签: windows-phone-7

<?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中获取Node?

我用:

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

}

但这不活跃。

2 个答案:

答案 0 :(得分: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());
        }
    }

答案 1 :(得分: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
}