我有一个XML,我想对多个具有相同值的元素进行分组。
<?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>>
,这不是问题。
问题是我想检查列表中的值,并查看是否存在特定值,例如在这种情况下为“第三标题”。因此,无论何时,在任何XML中,如果有“第三标题”,它将始终位于顶部,即[0],其余部分将在出现时出现。
我想知道如何自定义订单列表。
问候 阿曼
答案 0 :(得分:0)
在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());
}
}
}
没有字典的移动元素
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);
}
}
}
}
答案 1 :(得分:0)
不确定您所用的语法,但是这里是一个可以轻松适应的字符串列表的解决方案。
List<string> strings = new List<string>
{
"First title",
"Second title",
"Third title",
"Fourth title",
"Fifth title"
};
// Create a mask and convert it to List so we have .IndexOf()
var mask = new [] { "Third title" }.ToList();
// Bring items that match the mask on top and we don't care about the rest.
var sorted = strings.OrderByDescending(s => mask.IndexOf(s));
Console.WriteLine($"Sorted:\n{string.Join(",\n", sorted)}");
/*
Sorted:
Third title,
First title,
Second title,
Fourth title,
Fifth title
*/