简体   繁体   English

如何使用C#解析XML链接标签href属性

[英]How to parse xml link tag href attribute using c#

This is the sample xml of a feed item 这是提要项的示例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>

c# code for parsing xml elements 解析xml元素的C#代码

 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= ;
 }

I successfully parsed the xml elements except the atom:link tag url. 我成功地解析了除atom:link标记url之外的xml元素。
How we can parse the href property from the atom:link tag ? 我们如何从atom:link标记解析href属性?

Link has a namespace, you need to indicate it when parsing the XML. Link有一个名称空间,在解析XML时需要指出它。 I don't remember exactly what namespace atom is, but it should be indicated somewhere in the XML file (usually on the root node). 我不记得确切是什么名称空间atom ,但是应该在XML文件中的某个位置(通常在根节点上)指出它。 For instance, if it is: 例如,如果是:

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

Then you need to parse it like this: 然后,您需要像这样解析它:

rss.Link1 = (string)rssItem.Element(XName.Get("link", "http://www.w3.org/2005/Atom")).Attribute("href");

You need to specify the namespace when you look for the element: 查找元素时,需要指定名称空间:

XNamespace atom = "http://www.w3.org/2005/Atom";
...
rss.Link1 = rssItem.Element(atom + "link").Attribute("href").Value;

LINQ to XML makes namespace handling much simpler than any other XML API I've seen, but you still need to be aware of it. LINQ to XML使名称空间处理比我见过的任何其他XML API 简单得多,但是您仍然需要意识到这一点。 (I'm surprised the other elements aren't in a namespace, to be honest.) (老实说,我很惊讶其他元素不在命名空间中。)

I'd also transform your foreach loop into a LINQ query: 我还将您的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();

Also consider using a cast to string instead of the Value property, if some of the elements may be missing - that will set the relevant property to null, instead of throwing a NullReferenceException . 如果某些元素可能丢失,也可以考虑使用强制转换为string而不是Value属性,这会将相关属性设置为null,而不是抛出NullReferenceException

EDIT: If the link element is missing, you can fix that with: 编辑:如果缺少link元素,则可以使用以下方法修复该问题:

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

That will find the first href attribute within an atom link element, or use null - and then the cast to string will just return null if there's no attribute. 这将在一个原子link元素中找到第一个href属性,或使用null-然后如果没有属性,则强制转换为string将仅返回null (That's part of the user-defined conversion from XAttribute to string .) (这是用户定义的XAttributestring转换的一部分。)

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

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