选择节点 Linq to Xml C#

发布于 2024-11-28 03:00:46 字数 712 浏览 1 评论 0原文

XML 文件格式:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    <urlset>

我想选择所有“loc”节点(element1、element2),但这不起作用!

 foreach (XElement item in document.Elements("url").Descendants("loc")) // Change into what?
 {
      urlList.Add(item.Value);
 }

XML file format:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    <urlset>

I want to select all "loc" nodes (element1, element2), but this not work!!!

 foreach (XElement item in document.Elements("url").Descendants("loc")) // Change into what?
 {
      urlList.Add(item.Value);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-12-05 03:00:46

我怀疑问题是您要从 document.Elements("url") 而不是 document.Root.Elements("url")...所以它是正在查找 urlroot 元素,但该元素不存在。

试试这个:

List<string> urlList = doc.Root.Elements("url")
                               .Elements("loc")
                               .Select(x => (string) x)
                               .ToList();

请注意,我在这里没有使用“后代”,因为 loc 元素都直接位于 url 元素下方。

如果 only loc 元素无论如何都是正确的,您可以使用的另一种替代方法是:(

List<string> urlList = doc.Descendants("loc")
                          .Select(x => (string) x)
                          .ToList();

我假设 urlList 事先是空的。 ..对于这种情况,我喜欢使用 LINQ 进行整个操作,并消除只是添加到集合中的 foreach 循环。)

编辑:该代码对我有用。这是一个简短但完整的程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main(string[] args)
    {
        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    </urlset>";

        XDocument doc = XDocument.Parse(xml);
        List<string> urlList = doc.Root
                                  .Elements("url")
                                  .Elements("loc")
                                  .Select(x => (string) x)
                                  .ToList();
        Console.WriteLine(urlList.Count);
    }
}

I suspect the problem is that you're going from document.Elements("url") instead of document.Root.Elements("url")... so it's looking for a root element of url, which doesn't exist.

Try this:

List<string> urlList = doc.Root.Elements("url")
                               .Elements("loc")
                               .Select(x => (string) x)
                               .ToList();

Note that I haven't used Descendants here, as the loc elements are all directly beneath url elements anyway.

Another alternative you could use if the only loc elements are the right ones anyway, is just:

List<string> urlList = doc.Descendants("loc")
                          .Select(x => (string) x)
                          .ToList();

(I'm assuming urlList was empty beforehand... for this sort of situation I like to use LINQ for the whole operation and eliminate foreach loops that are just adding to a collection.)

EDIT: The code works for me. Here's a short but complete program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main(string[] args)
    {
        string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    </urlset>";

        XDocument doc = XDocument.Parse(xml);
        List<string> urlList = doc.Root
                                  .Elements("url")
                                  .Elements("loc")
                                  .Select(x => (string) x)
                                  .ToList();
        Console.WriteLine(urlList.Count);
    }
}
猫烠⑼条掵仅有一顆心 2024-12-05 03:00:46
var xDoc = XDocument.Parse(
    @"<urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    </urlset>");
var locElements = xDoc.Descendants("url").SelectMany(el => el.Descendants("loc"));
var xDoc = XDocument.Parse(
    @"<urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    </urlset>");
var locElements = xDoc.Descendants("url").SelectMany(el => el.Descendants("loc"));
瑾夏年华 2024-12-05 03:00:46

试试这个:

var query = for x in xDoc.Descendants("url")
            select (string)x.Element("loc");

foreach (string loc in query)
{
    urlList.Add(loc);
}

Try this:

var query = for x in xDoc.Descendants("url")
            select (string)x.Element("loc");

foreach (string loc in query)
{
    urlList.Add(loc);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文