Linq to XML - 使用命名空间获取元素

Posted

技术标签:

【中文标题】Linq to XML - 使用命名空间获取元素【英文标题】:Linq to XML - get element with namespace 【发布时间】:2022-01-22 04:08:23 【问题描述】:

我不知道如何在 item 元素中获取第二个链接元素。具有 xmlns="csx" 属性的那个。

我尝试指定命名空间,但没有成功。

XNamespace linkNamespace = XNamespace.Get("http://schemas.microsoft.com/rss/2007/contentsyncextensions");  

var item = tree.Descendant("item");
item.Element(linkNamespace + "link");

xml 是:

<rss version="2.0" xmlns:nd="http://schemas.newspaperdirect.com" xmlns:rx="http://schemas.microsoft.com/rss/2007/readerextensions" xmlns:csx="http://schemas.microsoft.com/rss/2007/contentsyncextensions">
    <channel>
        <title>The same is true for eurozone countries, assuming that the European Central Bank acts as the lender of last resort.</title>
        <link>http://library.pressdisplay.com/pressdisplay/viewer.aspx</link>
        <description>Search for : "The same is true for eurozone countries, assuming that the European Central Bank acts as the lender of last resort." (provided by PressDisplay.com)</description>
        <pubDate>Mon, 20 Dec 2021 00:00:00 GMT</pubDate>
        <lastBuildDate>Mon, 20 Dec 2021 01:21:44 GMT</lastBuildDate>
        <category>Total=9</category>
        <item rx:type="Story">
            <properties>
                <property key="newspaperTitle">Pakistan Today (Lahore)</property>
                <property key="newspaperCountry">Pakistan</property>
                <property key="newspaperLanguage">English</property>
            </properties>
            <page>8</page>
            <title>TIME TO OVERHAUL THE GLOBAL FINANCIAL SYSTEM</title>
            <link>http://library.pressdisplay.com/pressdisplay/pageview.aspx?issue=88502021120900000000001001&amp;page=8&amp;articleid=3077693453&amp;previewmode=2</link>
            <regionId>3077693453</regionId>
            <author>JEFFREY D SACHS Jeffrey D. Sachs, a professor at Columbia University, is director of the Center for Sustainable Development at Columbia University and president of the U.N. Sustainable Development Solutions Network.</author>
            <description>&lt;br/&gt;&lt;img  title="" src="https://i.prcdn.co/img?regionguid=32ed40cc-778d-43ef-9b20-3a78bc7fe73d&amp;scale=67&amp;file=88502021120900000000001001&amp;regionKey=VIFA7LohZIEEFfwEXTBRgQ%3d%3d"  style="float:left; margin-right:20px"/&gt; AT last month’s COP26 climate summit, hundreds of financial institutions declared that they would put trillions of dollars to work to finance solutions to climate change. Yet a major barrier stands in the way: The world’s financial system actually...</description>
            <pubDate>Thu, 09 Dec 2021 00:00:00 GMT</pubDate>
            <lastBuildDate xmlns="csx">Thu, 09 Dec 2021 00:00:00 GMT</lastBuildDate>
            <link xmlns="csx">http://library.pressdisplay.com/pressdisplay/services/rssarticleinfo.ashx?issue=88502021120900000000001001&amp;page=8&amp;articleid=3077693453</link>
            <pageimg xmlns="csx">https://i.prcdn.co/img?regionguid=32ed40cc-778d-43ef-9b20-3a78bc7fe73d&amp;scale=67&amp;file=88502021120900000000001001&amp;regionKey=VIFA7LohZIEEFfwEXTBRgQ%3d%3d</pageimg>
            <imageReferences xmlns="rx">
                <imageReference>
                    <caption>TIME TO OVERHAUL THE GLOBAL FINANCIAL SYSTEM</caption>
                    <credit>JEFFREY D SACHS Jeffrey D. Sachs, a professor at Columbia University, is director of the Center for Sustainable Development at Columbia University and president of the U.N. Sustainable Development Solutions Network.</credit>
                    <image  >
                        <link xmlns="csx">https://i.prcdn.co/img?regionguid=32ed40cc-778d-43ef-9b20-3a78bc7fe73d&amp;amp;scale=67&amp;amp;file=88502021120900000000001001&amp;amp;regionKey=VIFA7LohZIEEFfwEXTBRgQ%3d%3d</link>
                    </image>
                </imageReference>
            </imageReferences>
        </item>
    </channel>
</rss>

【问题讨论】:

【参考方案1】:

我认为 XML 不正确:

&lt;link xmlns="csx"&gt; 是命名空间“csx”中的元素名称“链接”,而不是命名空间“http://schemas.microsoft.com/rss/2007/contentsyncextensions”中。

&lt;csx:link&gt; 是正确的,如果命名空间“http://schemas.microsoft.com/rss/2007/contentsyncextensions”是指的。

如果真的是&lt;link xmlns="csx"&gt;,请使用XNamespace linkNamespace = XNamespace.Get("csx")

【讨论】:

以上是关于Linq to XML - 使用命名空间获取元素的主要内容,如果未能解决你的问题,请参考以下文章

LINQ to XML - 如何修复根元素中的默认命名空间

使用带有 Xml 命名空间的 Linq to Xml

XML名命空间

Linq to XML

LINQ to XML:如何在 C# 中获取元素值?

Atitit xml命名空间机制