简体   繁体   English

wp7 xdocument xml节点c#

[英]wp7 xdocument xml node c#

I need to extract an XML node of this XML document: 我需要提取此XML文档的XML节点:

http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad

I am unable to figure this out. 我无法弄清楚这一点。 Below is the current code I use to read in the XML from that url. 下面是我用来从该URL读取XML的当前代码。

private void button1_Click(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += HttpsCompleted;
    wc.DownloadStringAsync(new Uri("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad"));
}

private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
        this.textBox1.Text = xdoc.LastNode.ToString();
    }
}

How can I access the node lyric from within the XML document from the provided URL? 如何从提供的URL中访问XML文档中的节点lyric

Use Linq To Xml for this kind of scenario. 对于这种情况,请使用Linq To Xml。 Note, that there is a namespace ( xmlns="http://api.chartlyrics.com/" ) in the xml result. 请注意,xml结果中有一个名称空间( xmlns="http://api.chartlyrics.com/" )。

A really basic example could be : 一个非常基本的例子可能是:

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
        var lyric = xdoc.Descendants(XName.Get("Lyric","http://api.chartlyrics.com/")).FirstOrDefault();

        this.textBox1.Text = lyric.Value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM