如何对List< IGrouping< string,XElement>>进行排序按自定义顺序? [英] How do I sort a List<IGrouping<string, XElement>> in custom order?

查看:49
本文介绍了如何对List< IGrouping< string,XElement>>进行排序按自定义顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML,我想对具有相同值的多个元素进行分组.

I have an XML and I want to group multiple elements that have the same value.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1">
<heading><page num="1"/>First Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Second Title</title>
<break name="2-1">
<heading><page num="1"/>Second Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>First Title</title>
<break name="3-1">
<heading><page num="1"/>Third Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Third Title</title>
<break name="4-1">
<heading><page num="1"/>Fourth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
<break name="5-1">
<heading><page num="1"/>Fifth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
</body>
</repub>

我已经将值分组了,但是它是List<IGrouping<string, XElement>>格式,这不是问题.

I have already grouped the values but it is in List<IGrouping<string, XElement>> format and that is not the issue.

问题是我想检查列表中的值,并查看是否存在特定值,例如在这种情况下为第三标题".因此,无论何时,在任何XML中,如果有第三标题",它将始终位于顶部,即[0],其余部分将在出现时出现.

The issue is that I want to check for the values in the list and see if a particular value exists, say for example, in this scenario, "Third Title". So, whenever, in any XML, if there is Third Title, it will always be at the top, that is [0] and the rest will come as they occur.

我想知道如何自定义订购列表.

I want to know how to custom order my list.

问候 阿曼

推荐答案

在Xml Linq中使用字典:

Use a Dictionary with Xml Linq :

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

namespace ConsoleApplication103
{
    class Program
    {

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, XElement> dic = doc.Descendants("sec")
               .GroupBy(x => (string)x.Element("title"), y => y)
               .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }

}


没有字典的移动元素


Moving element without dictionary

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

namespace ConsoleApplication103
{
    class Program
    {

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement body = doc.Descendants("body").FirstOrDefault();

            List<XElement> results = body.Descendants("sec").Where(x => (string)x.Element("title") == "First Title").ToList();
            if (results != null)
            {
                List<XElement> breaks = results.SelectMany(x => x.Elements("break")).ToList();

                XElement newElement = XElement.Parse(results.FirstOrDefault().ToString());
                newElement.Elements("break").Remove();
                results.Remove();
                newElement.Add(breaks);
                body.AddFirst(newElement);
            }

        }
    }

}

这篇关于如何对List&lt; IGrouping&lt; string,XElement&gt;&gt;进行排序按自定义顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆