如何使用XDocument获取包含名称空间前缀的XML元素的值?

时间:2012-03-23 10:38:06

标签: xml windows-phone-7 linq-to-xml

我正在使用WP7应用程序中的Web服务,我需要解析的xml行是以下

<media:thumbnail url="http://pthumbnails.5min.com/10342750/517137484_c_embedStandard.jpg" width="480" height="352"/>

我正在使用此方法来解析剩余的XMl

foreach (var _item in channelNode.Elements("item"))
        {
            int id = int.Parse(_item.Element("id").Value);
            string title = _item.Element("title").Value;
            string description = _item.Element("description").Value;
            string studioName = _item.Element("studioName").Value;
            DateTime publicationDate = DateTime.Parse(_item.Element("pubDate").Value);
            string thumbnailUri = _item.Element("image").Element("url").Value;}

这是我要解析的完整项目

<item>
<id>517310163</id>
<title>
Walter Isaacson on Tips for Successful Entrepreneurship from Steve Jobs
</title>
<description>
Walter Isaacson discusses Steve Jobs' existence at the intersection of creativity and technology.
</description>
<link>
http://www.5min.com/Video/Walter-Isaacson-on-Tips-for-Successful-Entrepreneurship-from-Steve-Jobs-517310163
</link>
<studioName>Simon&Schuster</studioName>
<enclosure url="http://embed.5min.com/517310163/" duration="100" type="application/x-shockwave-flash"/>
<geoRestriction>ALL</geoRestriction>
<expirationDate/>
<media:community>
<media:statistics views="0"/>
</media:community>
<media:content url="http://avideos.5min.com//102/5173102/517310163_2.mp4" type="video/mp4" expression="sample" fileSize="174466031" duration="100" bitrate="600" lang="eng"/>
<media:title type="plain">
Walter Isaacson on Tips for Successful Entrepreneurship from Steve Jobs
</media:title>
<media:description type="plain">
Walter Isaacson discusses Steve Jobs' existence at the intersection of creativity and technology.
</media:description>
<media:thumbnail url="http://pthumbnails.5min.com/10346204/517310163_10.jpg" width="140" height="105" time="00:01:30"/>
<media:category label="Arts/Writing & Publishing">Arts/Writing & Publishing</media:category>
<media:credit role="author">SimonandSchuster</media:credit>
<media:rating>nonadult</media:rating>
<media:keywords>
Steve Jobs' Tips for Successful Entrepreneurship,apple company success,apple products,business advice,iPad development,market research,simon and schuster,steve jobs: the exclusive biography the exclusive,Walter Isaacson,Steve Jobs
<![CDATA[ Steve Jobs' Tips for Successful Entrepreneurship ]]>
</media:keywords>
<media:player url="http://www.5min.com/Video/Walter-Isaacson-on-Tips-for-Successful-Entrepreneurship-from-Steve-Jobs-517310163">
<![CDATA[
<div style='text-align:center'><object width='480' height='401' id='FiveminPlayer' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'><param name='allowfullscreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='movie' value='http://embed.5min.com/517310163/'/><embed name='FiveminPlayer' src='http://embed.5min.com/517310163/' type='application/x-shockwave-flash' width='480' height='401' allowfullscreen='true' allowScriptAccess='always'></embed></object><br/><a href='http://www.5min.com/Video/Walter-Isaacson-on-Tips-for-Successful-Entrepreneurship-from-Steve-Jobs-517310163' style='font-family: Verdana;font-size: 10px;' target='_blank'>Steve Jobs' Tips for Successful Entrepreneurship</a></div>
]]>
</media:player>
<pubDate>Thu, 22 Mar 2012 13:47:31 GMT</pubDate>
<image>
<title>
Walter Isaacson on Tips for Successful Entrepreneurship from Steve Jobs
</title>
<url>
http://pthumbnails.5min.com/10346204/517310163_10.jpg
</url>
<link>
http://www.5min.com/Video/Walter-Isaacson-on-Tips-for-Successful-Entrepreneurship-from-Steve-Jobs-517310163
</link>
<description>
Walter Isaacson discusses Steve Jobs' existence at the intersection of creativity and technology.
</description>
<width>150</width>
<height>100</height>
</image>
</item>

我应该如何获得媒体的价值:缩略图?

1 个答案:

答案 0 :(得分:0)

在XML的某处,您应该有一个名称空间声明,如:

xmlns:media="http://blah.org/stuff/"

您可以在解析XML时使用它,如下所示:

XNamespace media = "http://blah.org/stuff/";

string thumbnailUri = (string)_item.Element(media + "thumbnail").Attribute("url");

请注意,“url”是属性,而不是值。在下面的示例元素中,“SimonandSchuster”是值,而“role”是值为“author”的属性

<media:credit role="author">SimonandSchuster</media:credit>