G.Mich G.Mich - 25 days ago 20
C# Question

How can i deserialize Google News Sitemap Feed c#

I have a Google News Sitemap Feed but i can't deserialize xml on c# list collection.

I want get first 50 items from my feed.

What can i do for this ? Any idea ? Thanks

My xml sample is this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.hellenicshippingnews.com/wp-content/plugins/xml-sitemap-feed/includes/xsl/sitemap-news.xsl?ver=4.7.3"?>
<!-- generated-on="2016-11-07T12:40:55+00:00" -->
<!-- generator="XML & Google News Sitemap Feed plugin for WordPress" -->
<!-- generator-url="http://status301.net/wordpress-plugins/xml-sitemap-feed/" -->
<!-- generator-version="4.7.3" -->
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
http://www.google.com/schemas/sitemap-news/0.9
http://www.google.com/schemas/sitemap-news/0.9/sitemap-news.xsd
http://www.google.com/schemas/sitemap-image/1.1
http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd">
<url>
<loc>http://www.hellenicshippingnews.com/odc-to-convert-fifth-greek-vessel-olympic-target/</loc>
<news:news>
<news:publication>
<news:name>Hellenic Shipping News Worldwide</news:name>
<news:language>en</news:language>
</news:publication>
<news:publication_date>2016-11-07T10:00:57+00:00</news:publication_date>
<news:title>ODC to convert fifth Greek vessel Olympic Target</news:title>
<news:keywords>Hellenic Shipping News, ΒunkerportsnewsΠρώτηΣελιδα, Πρώτη σελιδα</news:keywords>
</news:news>
<image:image>
<image:loc>http://www.hellenicshippingnews.com/wp-content/uploads/2015/10/double-hulled_oil_tanker.jpg</image:loc>
<image:title><![CDATA[double-hulled_oil_tanker]]></image:title>
</image:image>
</url>
</urlset>


I try with this c# code but nothing happened:

XDocument feedXML = XDocument.Load("http://www.hellenicshippingnews.com/sitemap-news.xml");

var feeds = from feed in feedXML.Descendants("url")
select new
{
Title = feed.Element("loc").Value,
Link = feed.Element("news:title").Value,
Description = feed.Element("news:keywords").Value
};

Answer

You are forgetting about the namespaces (see those xmlns attributes around urlset root element).

Also, you should use Descendants method instead of Element

See notes in bold below:

Per documentation, Element:

Gets the first (in document order) child element with the specified XName.

And, Descendants:

Returns a collection of the descendant elements for this document or element, in document order.

A child element is considered a direct inner node of the parent element. title and keywords aren't child nodes of url, so you should use Descendants method to search deeper in the node hierarchy.

string ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
string news_ns = "http://www.google.com/schemas/sitemap-news/0.9";
var feeds = from feed in feedXML.Descendants(String.Format("{{{0}}}{1}", ns, "url"))
            select new
            {
                Title = feed.Element(String.Format("{{{0}}}{1}", ns, "loc")).Value,
                Link = feed.Descendants(String.Format("{{{0}}}{1}", news_ns, "title")).Single().Value,
                Description = feed.Descendants(String.Format("{{{0}}}{1}", news_ns, "keywords")).Single().Value
            };
Comments