c# - 如何在 C# 中按节点属性对 XML 文件进行排序

标签 c# asp.net xml visual-studio

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

我发现了无数线程来解释如何对 XML 文件中的节点进行排序。但是,我还没有找到任何关于如何根据公共(public)子节点属性对具有相同结构的多个 XML 文件进行排序的线程。

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

每个 XML 文件都有一个“项目”父节点和子节点“年”、“语言”和“作者”等。例如:

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

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

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

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

这有意义吗?有没有更聪明的方法来做到这一点?

最佳答案

排序

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

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 中的 XML
  • fileName 属性,包含 XML 文件的路径

假设在你的目标目录中你有这个 xml 文件:

0001.xml

<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>

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

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

关于c# - 如何在 C# 中按节点属性对 XML 文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539948/

相关文章:

android - 解析XML时出错; xml文件格式不正确( token 无效)

c# - typeof(object).TypeHandle.Value 的替代方案

c# - AutoMapper 如何根据上下文以不同方式将对象 A 映射到对象 B

asp.net - 如果有多个按钮,如何将 jquery 验证仅与一个按钮关联?

asp.net - 控件在运行时不显示

javascript - 如何在 MVC View 中呈现 XML 输出

java - 如何将我的数据加载到 GraphML 文件中以便于在 Prefuse 中使用?

c# - 使用 C# 调用 Azure 函数

c# - 拒绝访问 C :\program file\myapplicationfolder\

c# - 使用 LINQ 加密列数据