Linq to Xml 按标签分组

发布于 2024-12-17 19:08:58 字数 991 浏览 0 评论 0原文

我有一个与此类似的 xml:

<data>
<date>24/11</date>
<info>Info I want to get</info>
<info>Info I want to get</info>
<info>Info I want to get</info>

<date>25/11</date>
<info>Info I want to get</info>
<info>Info I want to get</info>
<info>Info I want to get</info>
</data>

问题是,我能够获取所有信息标签,但我获取所有日期的结果。

我不知道日期是什么,因为它们是动态生成的。我所知道的是,我可能有一个带有一两个日期标签的数据标签

我希望我可以在列表框中显示第一个日期的信息,在另一个列表框中显示第二个日期的信息。我怎样才能做到这一点?

期望的输出:

textbox with the first date

value of tag info related to the first date
value of tag info related to the first date
value of tag info related to the first date

如果有第二个日期,则也打印它:

textbox with the second date

value of tag info related to the second date
value of tag info related to the second date
value of tag info related to the second date

谢谢!

I have a xml similar to this one:

<data>
<date>24/11</date>
<info>Info I want to get</info>
<info>Info I want to get</info>
<info>Info I want to get</info>

<date>25/11</date>
<info>Info I want to get</info>
<info>Info I want to get</info>
<info>Info I want to get</info>
</data>

The thing is, Im able to get all the info tags, but I get the results from all dates.

I dont know what the dates will be, since the are dynamic generated. What I know is that I may have a data tag with one or two date tags

I wish I could show the first date´s info on a list box and the second date´s info on another listbox. How can I do that?

Desirable Output:

textbox with the first date

value of tag info related to the first date
value of tag info related to the first date
value of tag info related to the first date

If there´s a second date then print it too:

textbox with the second date

value of tag info related to the second date
value of tag info related to the second date
value of tag info related to the second date

Thx!

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

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

发布评论

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

评论(1

沐歌 2024-12-24 19:08:58

我不知道您想要填充哪种类型的列表框(例如 Windows 窗体、ASP.NET、WPF),因此我只是向您展示如何对 XML 输入进行分组:

    XDocument input = XDocument.Load("../../XMLFile4.xml");
    var groups =
        from info in input.Root.Elements("info")
        group info by info.ElementsBeforeSelf("date").Last() into g
        select new
        {
            date = (string)g.Key,
            infos = (from infoEl in g select (string)infoEl).ToList()
        };

    foreach (var item in groups)
    {
        Console.WriteLine("Date: {0}", item.date);
        foreach (string info in item.infos)
        {
            Console.WriteLine("\t{0}", info);
        }
    }

然后输出是例如

Date: 24/11
        Info I want to get
        Info I want to get
        Info I want to get
Date: 25/11
        Info I want to get
        Info I want to get
        Info I want to get

I don't know what kind of ListBox (e.g. Windows Forms, ASP.NET, WPF) you want to populate so I simply show you how to group that XML input:

    XDocument input = XDocument.Load("../../XMLFile4.xml");
    var groups =
        from info in input.Root.Elements("info")
        group info by info.ElementsBeforeSelf("date").Last() into g
        select new
        {
            date = (string)g.Key,
            infos = (from infoEl in g select (string)infoEl).ToList()
        };

    foreach (var item in groups)
    {
        Console.WriteLine("Date: {0}", item.date);
        foreach (string info in item.infos)
        {
            Console.WriteLine("\t{0}", info);
        }
    }

Output then is e.g.

Date: 24/11
        Info I want to get
        Info I want to get
        Info I want to get
Date: 25/11
        Info I want to get
        Info I want to get
        Info I want to get
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文