Parsing issues with Windows Phone7

2019-06-14 12:22发布

问题:

I'm working on an application with Windows Phone 7, which displays data from a specific URI, but it won't work. I'm stack,help me please. This is my XML :

<rss version="2.0">
   <channel>
      <title>info</title>
      <link>http://www.info.net</link>
      <description>Trouvez toutes les actualités en direct sur info.net ...</description>
      <language>fr</language>
      <copyright></copyright>
      <pubDate></pubDate>
      <image>
            <url></url>
            <title></title>
            <link>http://www.info.net</link>
            <width>200</width>
            <height>200</height>
          </image>
    <item>
<title>Actualités » Info News » News Régionales : Main info</title>
    <link>http://www.info.net/fr/actualite/actualites_info-news_news-regionales/my-main-info/54</link>
    <pubDate>Thu, 29 Dec 2011 00:22:00 +0100</pubDate>
    <description><![CDATA[<img align='left' width='139' src='http://www.info.net/uploads/content/thumbnails/2011122902313__news.jpg'> My main info details : ...]]></description> 
</item><item>
    .
    .
    .
      </item></channel></rss>

And i want to display a list that contains:

Main info (title)
http://www.info.net/uploads/content/thumbnails/2011122902313__news.jpg (description)
My main info details (description)

This is my C# code:

  var doc = XDocument.Load(new StringReader(e.Result));
            var items = from c in doc.Descendants("item")
                    select new RSSitem()
                    {
                        Title = c.Element("title").Value,
                        Photo = c.Element("img").Attribute("src").Value,
                        Description = c.Element("description").Value,
                        Link = c.Element("link").Value,
                    };
            ListBoxNews.ItemsSource = items;

回答1:

The <img> tag is not part of your XML document, but an HTML element in a CDATA node of the description element.

To extract it, you'll need to use the HtmlAgilityPack (HtmlAgilityPack on NuGet).

Here's an updated version of your code:

(I converted your LINQ expression to use the extension methods as sequential code doesn't work well in LINQ expressions)

var items = doc.Descendants("item")
               .Select(c =>
               {
                    string descriptionHtml = c.Element("description").Value;
                    HtmlDocument descriptionDoc = new HtmlDocument();

                    descriptionDoc.LoadHtml(descriptionHtml);

                    HtmlNode imageNode = doc.DocumentNode.SelectSingleNode("//img[@src]");

                    string imageUrl = (imageNode != null)
                        ? imageNode.GetAttributeValue("src", null)
                        : null;

                    // This might need further looking at, depending on your HTML
                    string description = doc.DocumentNode.InnerText;

                    return new RSSitem() 
                    { 
                        Title = c.Element("title").Value, 
                        Photo = imageUrl, 
                        Description = description, 
                        Link = c.Element("link").Value, 
                    };

               }).ToList();


回答2:

for c data node something like

XmlNode cDataNode = doc.SelectSingleNode("channel/description").ChildNodes[0]; 

string finalData = cDataNode.InnerText.Trim();