Don't ask someone to code this solution for me - just look for recommendations on the best approach. I am working on a .aspx file in VS2015 using C # code.
I have found countless threads explaining how to sort nodes in an XML file. But I did not find a single topic on how to sort multiple XML files with the same structure according to the common child attribute of node.
My situation: I have a directory of hundreds of XML files with the name, 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 the node child.
Each XML file has a parent element "item" 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>
If instead of listing the files in order from 0001 to 6400, I instead want to list them alphabetically according to the item / author node @sortby attribute, how would I do this?
One of my ideas was to create a temporary XML file that collects the information needed for each XML file. Then I can sort the temporary XML file and then scroll through the nodes to display the files in the correct order. Something like that...
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
It makes sense? Is there a smarter way to do this?
source
share