XDocument.Descendants(itemName)-查找合格名称时出现问题 [英] XDocument.Descendants(itemName) - Problems finding qualified name
问题描述
我正在尝试从网站上读取XML-RSS-Feed.因此,我使用异步下载并使用XDocument.Parse()
方法创建XDocument
.
I'm trying to read a XML-RSS-Feed from a website. Therefore I use a async download and create a XDocument
with the XDocument.Parse()
Method.
文档旨在非常简单,就像这样:
The Document intends to be very simple, like this:
<root>
<someAttribute></SomeAttribute>
<item>...</item>
<item>...</item>
</root>
现在我想读出所有项目.因此,我尝试了:
Now I want to read out all the items. Therefore I tried:
foreach (XElement NewsEntry in xDocument.Descendants("item"))
但这是行不通的.因此,我在此公告板上找到了一个使用限定名称的帖子,因为在根元素中定义了一些名称空间:
but this doesn't work. So I found a post in this board to use the qualified name, because there are some namespaces defined in the root element:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns="http://purl.org/rss/1.0/">
好吧,我尝试了所有3种可用的名称空间-对我没有任何帮助:
well, I tried all 3 available namespaces - nothing worked for me:
XName itemName = XName.Get("item", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
XName itemName2 = XName.Get("item", "http://purl.org/dc/elements/1.1/");
XName itemName3 = XName.Get("item", "http://purl.org/rss/1.0/modules/syndication/");
任何帮助将不胜感激. (通常,我正在使用Regex进行XML分析-但这一次我正在为移动设备进行开发,因此需要关心性能.)
Any help would be appreciated. (Usually I'm doing the XML-Analysis with Regex - but this time I'm developing for a mobile device, and therefore need to care about performance.)
推荐答案
您尚未尝试默认命名空间位于rdf
声明的末尾:
You have not tried the default namespace at the end of the rdf
declaration:
xmlns="http://purl.org/rss/1.0/"
这是有道理的,因为默认名称空间中的任何元素都不需要将名称空间放在元素名称之前.
This makes sense, as any element in the default namespace will not need to have the namespace prepended to the element name.
这篇关于XDocument.Descendants(itemName)-查找合格名称时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!