我如何反序列化 Google News Sitemap Feed c#
How can i deserialize Google News Sitemap Feed c#
我有一个 Google 新闻站点地图提要,但我无法在 c# 列表集合上反序列化 xml。
我想从我的供稿中获取前 50 件商品。
我能为此做什么?任何想法 ?谢谢
我的 xml 样本是这样的:
<?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>
我尝试使用此 C# 代码但没有任何反应:
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
};
您忘记了名称空间(请参阅 urlset
根元素周围的那些 xmlns
属性)。
此外,您应该使用 Descendants method instead of Element
请参阅下面的粗体注释:
根据文档,Element
:
Gets the first (in document order) child element with the specified XName.
并且,Descendants
:
Returns a collection of the descendant elements for this document or element, in document order.
子元素被认为是父元素的直接内部节点。 title
和 keywords
不是 url
的子节点,因此您应该使用 Descendants
方法在节点层次结构中更深入地搜索。
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
};
我有一个 Google 新闻站点地图提要,但我无法在 c# 列表集合上反序列化 xml。
我想从我的供稿中获取前 50 件商品。
我能为此做什么?任何想法 ?谢谢
我的 xml 样本是这样的:
<?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>
我尝试使用此 C# 代码但没有任何反应:
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
};
您忘记了名称空间(请参阅 urlset
根元素周围的那些 xmlns
属性)。
此外,您应该使用 Descendants method instead of Element
请参阅下面的粗体注释:
根据文档,Element
:
Gets the first (in document order) child element with the specified XName.
并且,Descendants
:
Returns a collection of the descendant elements for this document or element, in document order.
子元素被认为是父元素的直接内部节点。 title
和 keywords
不是 url
的子节点,因此您应该使用 Descendants
方法在节点层次结构中更深入地搜索。
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
};