-3

我已经看到了这个问题,但我没有看到答案..

所以我得到这个错误:

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

在此代码上:

    XDocument XMLFeed = XDocument.Load("http://feeds.foxnews.com/foxnews/most-popular?format=xml");
    XNamespace content = "http://purl.org/rss/1.0/modules/content/";

    var feeds = from feed in XMLFeed.Descendants("item")
        select new
        {
            Title = feed.Element("title").Value,
            Link = feed.Element("link").Value,
            pubDate = feed.Element("pubDate").Value,
            Description = feed.Element("description").Value,
            MediaContent = feed.Element(content + "encoded")
        };

    foreach (var f in feeds.Reverse())
    {
        ....
    }

一个项目看起来像这样:

<rss>    
<channel>

....items....

<item>
<title>Pentagon confirms plan to create new spy agency</title>
<link>http://feeds.foxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/</link>
<category>politics</category>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/" />
<pubDate>Tue, 24 Apr 2012 12:44:51 PDT</pubDate>
<guid isPermaLink="false">http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</guid>
<content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[|http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg<img src="http://feeds.feedburner.com/~r/foxnews/most-popular/~4/lVUZwCdjVsc" height="1" width="1"/>]]></content:encoded>
<description>The Pentagon confirmed Tuesday that it is carving out a brand new spy agency expected to include several hundred officers focused on intelligence gathering around the world.&amp;amp;#160;</description>
<dc:date xmlns:dc="http://purl.org/dc/elements/1.1/">2012-04-4T19:44:51Z</dc:date>
<feedburner:origLink>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</feedburner:origLink>
</item>

....items....

</channel>
</rss>    

我想要的只是获取“http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg”,然后在此之前检查 content:encoded 是否存在..

谢谢。

编辑: 我找到了一个示例,我可以显示和编辑尝试处理它的代码..

EDIT2: 我以丑陋的方式做到了:

text.Replace("content:encoded", "contentt").Replace("xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"","");

然后以正常方式获取元素:

MediaContent = feed.Element("contentt").Value
4

2 回答 2

0

您应该使用 XNamespace:

XNamespace content = "...";

// later in your code ...
MediaContent = feed.Element(content + "encoded")

在此处查看更多详细信息。

(当然,您要分配给内容的字符串与中相同xmlns:content="...")。

于 2012-04-25T11:55:59.147 回答
0

以下代码

    static void Main(string[] args)
    {

            var XMLFeed = XDocument.Parse(
@"<rss>    
<channel>

....items....

<item>
<title>Pentagon confirms plan to create new spy agency</title>
<link>http://feeds.foxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/</link>
<category>politics</category>
<dc:creator xmlns:dc='http://purl.org/dc/elements/1.1/' />
<pubDate>Tue, 24 Apr 2012 12:44:51 PDT</pubDate>
<guid isPermaLink='false'>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</guid>
<content:encoded xmlns:content='http://purl.org/rss/1.0/modules/content/'><![CDATA[|http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg<img src='http://feeds.feedburner.com/~r/foxnews/most-popular/~4/lVUZwCdjVsc' height='1' width='1'/>]]></content:encoded>
<description>The Pentagon confirmed Tuesday that it is carving out a brand new spy agency expected to include several hundred officers focused on intelligence gathering around the world.&amp;amp;#160;</description>
<dc:date xmlns:dc='http://purl.org/dc/elements/1.1/'>2012-04-4T19:44:51Z</dc:date>
<!-- <feedburner:origLink>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</feedburner:origLink> -->
</item>

....items....

</channel>
</rss>");
            XNamespace contentNs = "http://purl.org/rss/1.0/modules/content/";
            var feeds = from feed in XMLFeed.Descendants("item")
                        select new
                                   {
                                       Title = (string)feed.Element("title"),
                                       Link = (string)feed.Element("link"),
                                       pubDate = (string)feed.Element("pubDate"),
                                       Description = (string)feed.Element("description"),
                                       MediaContent = GetMediaContent((string)feed.Element(contentNs + "encoded"))
                                   };
            foreach(var item in feeds)
            {
                Console.WriteLine(item);
            }
        }

        private static string GetMediaContent(string content)
        {
            int imgStartPos = content.IndexOf("<img");
            if(imgStartPos > 0)
            {
                int startPos = content[0] == '|' ? 1 : 0;

                return content.Substring(startPos, imgStartPos - startPos);
            }

            return string.Empty;
        }

结果是:

{ 标题 = 五角大楼确认创建新间谍机构的计划,链接 = http://feeds.f
oxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/,pubDate = 2012 年 4 月 24 日星期二 1
2:44:51 PDT,描述 = 五角大楼周二证实,它正在削减
 一个全新的间谍机构,预计将包括数百名专注于
全球情报收集。 , MediaContent = http://global
.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg }
按任意键继续 。. .

几点:

  • 您永远不想将 Xml 视为文本 - 在您的情况下,您删除了命名空间声明,但实际上如果命名空间被声明为内联(即没有绑定到前缀)或将定义不同的前缀,您的代码将无法工作,即使语义上两个文档将是等效的
  • 除非您知道 CDATA 中的内容以及如何处理它,否则您总是希望将其视为文本。如果你知道它是别的东西,你可以在解析后以不同的方式对待它 - 有关更多详细信息,请参阅下面我对 CDATA 的详细说明
  • 为了避免在元素丢失时出现 NullReferenceExceptions,我使用了显式转换运算符(字符串)而不是调用 .Value
  • 您发布的 Xml 不是有效的 xml - feedburner 前缀缺少命名空间 Uri

这不再与问题有关,但可能对某些人有帮助,所以我离开了

就编码元素的内容而言,它在 CDATA 部分内。CDATA 部分中的内容不是 Xml,而是纯文本。CDATA 通常用于不必编码 '<'、'>'、'&' 字符(如果没有 CDATA,它们必须编码为 < > 和 & 以不破坏 Xml 文档本身),但 Xml 处理器将字符处理为CDATA 就好像它们已被编码(或更正确地对它们进行编码)。如果您想嵌入 html,CDATA 很方便,因为嵌入的内容在文本上看起来像原始内容,但如果 html 不是格式良好的 Xml,它不会破坏您的 xml。由于 CDATA 内容不是 Xml 而是文本,因此不可能将其视为 Xml。您可能需要将 is 视为文本并使用例如正则表达式。它是一个有效的 Xml,您可以再次将内容加载到 XElement 并对其进行处理。在您的情况下,您的内容混合在一起,因此除非您使用一些肮脏的技巧,否则这并不容易。如果您只有一个顶级元素而不是混合内容,那么一切都会很容易。hack 是添加元素以避免所有麻烦。在 foreach 外观中,您可以执行以下操作:

var mediaContentXml = XElement.Parse("<content>" + (string)item.MediaContent + "</content>");
Console.WriteLine((string)mediaContentXml.Element("img").Attribute("src"));    

同样,它并不漂亮,而且它是一种 hack,但如果编码元素的内容是有效的 Xml,它将起作用。更正确的做法是让我们将 ConformanceLevel 设置为 Fragment 的 XmlReader 并适当地识别各种节点以创建相应的 Linq to Xml 节点。

于 2012-04-25T20:23:47.440 回答