Jerome De San Nicolás

Sorting Materials in c#

Discussion created by Jerome De San Nicolás on Nov 27, 2018
Latest reply on Jun 18, 2019 by Jerome De San Nicolás


I have seen some post complaining on the impossibility to sort the material database by name.

As I'm learning the c# I've make a little code to sort the database that I want to share with you.

It seems to work correctly but the process is kind of wanky. (you have to rename files extensions )


Be sure to make a BACKUP before testing as I'm a noob developer. And to leave a feedback on what could I improve.




Ps the code :


using System.Linq;
using System.Xml.Linq;

namespace SortXlm
class Program
static void Main(string[] args)
//Open the xml doc
XDocument doc = XDocument.Load("C:\\NAME OF THE BASE.XML");
//you have to change the extention of the material batabase from .sldmat to .xml (MAKE BACKUP!)

//sort the stuff

//save xml in another file
doc.Save("C:\\NEW SORTED XML.xml");
//you have to change the extention of the material batabase from .xml to .sldmat and put the file in the library directory

static void SortByName(XContainer container)

from childEl in container.Elements()
orderby childEl.Attributes("name").Select(a => a.Value).FirstOrDefault()
select childEl

foreach (XElement childEl in container.Elements().Where(e => e.HasElements))
if (childEl.Name == "classification" || childEl.Name == "material")