Linq to XML - 获取带有命名空间的元素

Linq to XML - get element with namespace

我不知道如何获取 item 元素中的第二个 link 元素。具有 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 alt="" title="" src="https://i.prcdn.co/img?regionguid=32ed40cc-778d-43ef-9b20-3a78bc7fe73d&amp;scale=67&amp;file=88502021120900000000001001&amp;regionKey=VIFA7LohZIEEFfwEXTBRgQ%3d%3d" width="80" 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 height="120" width="80">
                        <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>

我想 XML 不正确:

<link xmlns="csx"> 是名称空间“csx”中的元素名称“link”,而不是名称空间“http://schemas.microsoft.com/rss/2007/contentsyncextensions”中的元素名称。

<csx:link> 是正确的,如果名称空间“http://schemas.microsoft.com/rss/2007/contentsyncextensions”是指。

如果确实是 <link xmlns="csx">,请使用 XNamespace linkNamespace = XNamespace.Get("csx")