.net 4.0 名称中不能包含十六进制值0x3A的“:”字符

.net 4.0 名称中不能包含十六进制值0x3A的“:”字符,.net-4.0,xml-parsing,linq-to-xml,.net 4.0,Xml Parsing,Linq To Xml,我已经看到这个问题了,但没有找到答案 所以我得到了这个错误: 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.

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

所以我得到了这个错误:

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>    
然后以正常方式获取元素:

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

您应该使用XNamespace:

XNamespace content = "...";

// later in your code ...
MediaContent = feed.Element(content + "encoded")
请参阅更多详细信息

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

以下代码

    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;
        }
结果:

有几点:

您永远不希望将Xml视为文本—在您的例子中,您删除了名称空间声明,但实际上,如果名称空间是内联声明的,即未绑定到前缀或将定义不同的前缀,则即使这两个文档在语义上是等效的,您的代码也无法工作 除非您知道CDATA中的内容以及如何处理它,否则您总是希望将其视为文本。如果您知道它是其他东西,您可以在解析后以不同的方式对待它-有关更多详细信息,请参阅下面我详细介绍的CDATA 为了避免元素丢失时出现NullReferenceException,我使用了显式转换运算符字符串,而不是调用.Value 您发布的Xml不是有效的Xml-feedburner前缀缺少命名空间Uri 这不再与问题有关,但可能对某些人有帮助,因此我将离开它

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


同样,它并不漂亮,而且是一种黑客行为,但如果编码元素的内容是有效的Xml,它就会起作用。更正确的方法是将ConformanceLevel设置为Fragment并正确识别所有类型的节点,以创建相应的Linq to Xml节点

嗯,你可以在非根元素上导入名称空间吗?我真的不太懂xml解析来回答你。但是,正如我所说的,这只是一个项目-上面有一个和元素..可能是锯的重复,并尝试了Vlad的回答-两个都不起作用。可能是重复的。@nir:到底是什么?feed.Element?在foreach f.MediaContent中,例如,如果xmlns:content=则如果未找到子元素,则返回XNamespace content=@nir::null。您确定您的命名空间是正确的吗?尝试转储该项并查看元素是否存在。这个示例只是从提要粘贴。我不明白您建议如何解决我的内容问题:encodedI在您的Xml中找不到,问题的标题是关于名称空间的。无论如何,我假设您正在查找编码元素的CDATA部分中的image元素的src属性的值。我相应地更新了我的回答。我修复了问题几次,但没有更新,我现在更新了。我不想要src内部的内容,而是想要“|”和“你只需要解析它”之间的内容。我更新了代码,结果是一致的,因为它只在content:encoded中看到CDATA,所以它将null返回到MediaContent?非常感谢!
    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;
        }
{ Title = Pentagon confirms plan to create new spy agency, Link = http://feeds.f oxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/, pubDate = Tue, 24 Apr 2012 1 2:44:51 PDT, 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.&#160;, MediaContent = http://global .fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg } Press any key to continue . . .
var mediaContentXml = XElement.Parse("<content>" + (string)item.MediaContent + "</content>");
Console.WriteLine((string)mediaContentXml.Element("img").Attribute("src"));