中按节点属性对XML文件进行排序

中按节点属性对XML文件进行排序

本文介绍了如何在C#中按节点属性对XML文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要求任何人为我编写此解决方案-只是寻找最佳方法的指导.我正在使用2015年之后的C#代码在VS2015中处理.aspx文件.

Not asking for anyone to code this solution for me - just looking for guidance on the best approach. I'm working on an .aspx file in VS2015 using C# code behind.

我发现了无数线程来解释如何对XML文件中的节点进行排序.但是,根据通用的子节点属性,我没有找到有关如何对具有相同结构的多个XML文件进行排序的任何线索.

I've found countless threads explaining how to sort nodes within an XML file. But, I have not found any threads on how to sort multiple XML files with the same structure, according to a common child node attribute.

我的情况:我有一个包含数百个XML文件的目录,简单地命名为0001.xml到6400.xml.每个XML文件都具有相同的结构.我想根据子节点的属性对文件(而不是节点)进行排序.

My situation: I have a directory of hundreds of XML files named, simply, 0001.xml through 6400.xml. Each XML file has the same structure. I want to sort the files (not the nodes) according to the attribute of a child node.

每个XML文件都有一个"item"父节点,还有一个"year","language"和"author"等子节点.例如:

Each XML file has an "item" parent node and has child nodes "year", "language", and "author", among others. For example:

<item id="0001">
   <year>2011</year>
   <language id="English" />
   <author sortby="Smith">John F. Smith</author>
   <content></content>
</item>

如果不是要按照项目/作者节点的@sortby属性按字母顺序列出文件,而不是按0001到6400的顺序列出文件,我该怎么做?

If, instead of listing the files in order 0001 thru 6400, I instead want to list them in alphabetical order according to the item/author node's @sortby attribute, how would I do that?

我想到的一个想法是创建一个临时XML文件,该文件收集每个XML文件中所需的信息.然后,我可以对临时XML文件进行排序,然后遍历节点以正确的顺序显示文件.像这样...

One idea that I had was to create a temporary XML file that gathers the information needed from each XML file. Then, I can sort the temporary XML file and then loop through the nodes to display the files in the proper order. Something like this...

XDocument tempXML = new XDocument();
// add parent node of <items>

string[] items = Directory.GetFiles(directory)
foreach (string item in items)
{
   // add child node of <item> with attributes "filename", "year", "language", and "author"
}

// then sort the XML nodes according to attributes

这有意义吗?有更聪明的方法吗?

Does this make sense? Is there a smarter way to do this?

推荐答案

排序

我们可以使用以下代码显示使用LINQ to Xml排序的xml文件:

Sorting

We can show xml files sorted using a bit of LINQ to Xml, with this following code:

var xmlsWithFileName = Directory.GetFiles(directory)
                                .Select(fileName => new { fileName, xml = XDocument.Parse(File.ReadAllText(fileName)) })
                                .OrderBy(tuple => tuple.xml.Element("item").Element("author").Attribute("sortby").Value);

xmlsWithFileName 的每个元素都将具有

  • xml属性,该属性在XDocument中包含de XML
  • fileName属性,其中包含XML文件的路径

假设您的目标目录中有以下xml文件:

Assuming that in your target directory you have this xml files:

<item id="0001">
   <year>2011</year>
   <language id="English" />
   <author sortby="Smith">John F.Smith</author>
   <content></content>
</item>

0002.xml

<item id="0002">
   <year>2012</year>
   <language id="Portuguese" />
   <author sortby="Monteiro">Alberto Monteiro</author>
   <content></content>
</item>

您可以使用此代码进行测试

You can use this code to test

public static void ShowXmlOrderedBySortByAttribute(string directory)
{
    var xmlsWithFileName = Directory.GetFiles(directory)
                                    .Select(fileName => new { fileName, xml = XDocument.Parse(File.ReadAllText(fileName)) })
                                    .OrderBy(tuple => tuple.xml.Element("item").Element("author").Attribute("sortby").Value);

    foreach (var xml in xmlsWithFileName)
    {
        Console.WriteLine($"Filename: {xml.fileName}{Environment.NewLine}Xml content:{Environment.NewLine}");
        Console.WriteLine(xml.xml.ToString());
        Console.WriteLine("================");
    }
}

这段代码的输出是:

Filename: c:\temp\teste\0002.xml
Xml content:

<item id="0002">
  <year>2012</year>
  <language id="Portuguese" />
  <author sortby="Monteiro">Alberto Monteiro</author>
  <content></content>
</item>
================
Filename: c:\temp\teste\0001.xml
Xml content:

<item id="0001">
  <year>2011</year>
  <language id="English" />
  <author sortby="Smith">John F.Smith</author>
  <content></content>
</item>
================

如您所见, XML 0002.xml出现在第一位置,然后是0001.xml

As you can see, the XML 0002.xml appear in first position, then the 0001.xml

这篇关于如何在C#中按节点属性对XML文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:13