Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c解析xml链接标记href属性#_C#_Xml_Windows Phone 8_Rss - Fatal编程技术网

C# 如何使用c解析xml链接标记href属性#

C# 如何使用c解析xml链接标记href属性#,c#,xml,windows-phone-8,rss,C#,Xml,Windows Phone 8,Rss,这是提要项的示例xml <item> <pubDate>2013-12-11 10:28:55</pubDate> <title> SAG Awards Nominations: 12 Years a Slave, Breaking Bad lead the race </title> <link> http://www.rottentomatoes.com/m/1929182/

这是提要项的示例xml

 <item>
   <pubDate>2013-12-11 10:28:55</pubDate>
   <title>
     SAG Awards Nominations: 12 Years a Slave, Breaking Bad lead the race
   </title>
   <link>
     http://www.rottentomatoes.com/m/1929182/news/1929182/
   </link>
   <description>
   <![CDATA[ ]]>
   </description>
   <atom:link rel="thumbnail" type="image/*"  href="http://content6.flixster.com/movie/11/17/36/11173600_tmb.jpg"/>
  </item>

2013-12-11 10:28:55
SAG奖提名:12年奴隶,打破坏习惯,领跑比赛
http://www.rottentomatoes.com/m/1929182/news/1929182/
c#解析xml元素的代码

 List<XElement> elementsList = xmlItems.Descendants("item").ToList();
 foreach (XElement rssItem in elementsList)
 {
    RSSItem rss = new RSSItem();
    rss.Description1 = rssItem.Element("description").Value;
    rss.Link1 = rssItem.Element("link").Value;
    rss.Title1 = rssItem.Element("title").Value;
    rss.ImageUrl= ;
 }
List elementsList=xmlItems.subjects(“item”).ToList();
foreach(元素列表中的元素rssItem)
{
RSSItem rss=新的RSSItem();
rss.Description1=rssItem.Element(“description”).Value;
rss.Link1=rssItem.Element(“link”).Value;
rss.Title1=rssItem.Element(“title”).Value;
rss.ImageUrl=;
}
我成功地解析了除
atom:link
标记url之外的xml元素。

如何从
atom:link
标记解析href属性

链接有一个名称空间,在解析XML时需要指出它。我记不清atom的名称空间是什么,但应该在XML文件中的某个地方指明它(通常在根节点上)。例如,如果是:

<feed xmlns:atom="http://www.w3.org/2005/Atom">

查找元素时,需要指定命名空间:

XNamespace atom = "http://www.w3.org/2005/Atom";
...
rss.Link1 = rssItem.Element(atom + "link").Attribute("href").Value;
LINQtoXML使名称空间处理比我见过的任何其他XMLAPI都简单得多,但您仍然需要注意它。(老实说,我很惊讶其他元素不在名称空间中。)

我还将您的
foreach
循环转换为LINQ查询:

var items = xmlItems.Descendants("item")
                    .Select(x => new RSSItem {
                         Description1 = x.Element("description").Value,
                         Link1 = x.Element(atom + "link").Attribute("href").Value,
                         Title1 = x.Element("title").Value,
                         ...
                     })
                    .ToList();

也考虑使用CAST到代码>字符串,而不是<代码>值>代码>属性,如果某些元素可能丢失-将将相关属性设置为NULL,而不是抛出<代码> Null ReavigyExp><代码> >

编辑:如果缺少
链接
元素,可以使用以下方法修复此问题:

Link1 = (string) x.Elements(atom + "link").Attributes("href").FirstOrDefault()

这将在atom
链接
元素中找到第一个
href
属性,或者使用null-然后转换到
字符串
将只返回
null
,如果没有属性。(这是从
XAttribute
string
的用户定义转换的一部分)

谢谢您的回答。我们如何处理那里的空引用?就个人而言,我更喜欢使用
XName+(XNamespace,string)
构造
XName
s-我发现它比调用
XName容易得多,说实话,在我读到你的答案之前,我甚至不知道你可以连接一个XNamespace和一个XName。既然某些项没有atom标记,我们如何处理空引用?
Link1 = (string) x.Elements(atom + "link").Attributes("href").FirstOrDefault()