【问题标题】:Parse xml with XElement使用 XElement 解析 xml
【发布时间】:2013-01-29 12:31:57
【问题描述】:

我有这个XElement

<item>
  <guid isPermaLink="false">http://gdata.youtube.com/feeds/api/videos/s1tAYmMjLdY</guid>
  <pubDate>Wed, 17 Jun 2009 05:23:10 +0000</pubDate>
  <atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-01-29T11:28:32.000Z</atom:updated>
  <app:control xmlns:app="http://purl.org/atom/app#">
    <yt:state name="restricted" reasonCode="limitedSyndication" xmlns:yt="http://gdata.youtube.com/schemas/2007">Syndication of this video was restricted.</yt:state>
  </app:control>
  <category domain="http://schemas.google.com/g/2005#kind">http://gdata.youtube.com/schemas/2007#video</category>
  <category domain="http://gdata.youtube.com/schemas/2007/categories.cat">Music</category>
  <title>blink-182 - I Miss You</title>
  <description>Music video by blink-182 performing I Miss You. (C) 2003 Geffen Records</description>
  <link>http://www.youtube.com/watch?v=s1tAYmMjLdY&amp;feature=youtube_gdata</link>
  <author>blink182VEVO</author>
  <gd:comments xmlns:gd="http://schemas.google.com/g/2005">
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/s1tAYmMjLdY/comments" countHint="46136" />
  </gd:comments>
  <media:group xmlns:media="http://search.yahoo.com/mrss/">
    <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
    <media:content url="http://www.youtube.com/v/s1tAYmMjLdY?version=3&amp;f=videos&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="230" yt:format="5" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
    <media:description type="plain">Music video by blink-182 performing I Miss You. (C) 2003 Geffen Records</media:description>
    <media:keywords />
    <media:player url="http://www.youtube.com/watch?v=s1tAYmMjLdY&amp;feature=youtube_gdata_player" />
    <media:restriction type="country" relationship="deny">DE</media:restriction>
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/0.jpg" height="360" width="480" time="00:01:55" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/1.jpg" height="90" width="120" time="00:00:57.500" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/2.jpg" height="90" width="120" time="00:01:55" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/3.jpg" height="90" width="120" time="00:02:52.500" />
    <media:title type="plain">blink-182 - I Miss You</media:title>
    <yt:duration seconds="230" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
  </media:group>
  <gd:rating average="4.93231" max="5" min="1" numRaters="154927" rel="http://schemas.google.com/g/2005#overall" xmlns:gd="http://schemas.google.com/g/2005" />
  <yt:statistics favoriteCount="0" viewCount="47338118" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
</item>

我想得到media:group

XElement mediaGroup = element.Element("media:group");

但我收到此错误:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

我该如何解决?

【问题讨论】:

    标签: c# windows-phone-7 linq-to-xml xelement


    【解决方案1】:

    您需要单独指定命名空间 - 您不能直接从字符串转换为非本地 XName。 LINQ to XML 使用从stringXNamespace 的隐式转换,然后使用重载的+ 运算符从XNamespace + string 创建XName,使这变得容易。

    例如:

    XNamespace media = "http://search.yahoo.com/mrss/";
    XElement mediaGroup = element.Element(media + "group");
    

    (根据 Cédric 的回答,您可以使用 XName.Get,但由于您可能在多个位置使用相同的命名空间,我更喜欢这样拆分它。)

    【讨论】:

    • @MTA:嗯,是的,然后您使用mediaGroup.Element(media + "title") 并使用显式转换为string,或者使用Value 属性。
    【解决方案2】:

    : 将元素名称拆分为前缀(命名空间的,此处为:http://search.yahoo.com/mrss/)和本地名称。

    请改用:XName.Get("group", "http://search.yahoo.com/mrss/")

    string mediaNamespace = "http://search.yahoo.com/mrss/";
    XElement mediaGroup = element.Element(XName.Get("group", mediaNamespace));
    

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多