// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; using System.Xml; namespace E { public class Model { private int id; private string name; private int year; public Model() : this(0, "", 0) { } public Model(int id, string name, int year) { this.id = id; this.name = name; this.year = year; } public int Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public int Year { get { return year; } set { year = value; } } public override string ToString() { return ("[" + id + "," + name + "," + year + "]"); } } public class ModelNameComparer : IComparer { int IComparer.Compare(Object o1, Object o2) { return String.Compare(((Model)o1).Name,((Model)o2).Name); } } public class ModelYearComparer : IComparer { int IComparer.Compare(Object o1, Object o2) { return (((Model)o1).Year - ((Model)o2).Year); } } class MainClass { public static void Main(string[] args) { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\biler.xml"); XmlNodeList models = doc.GetElementsByTagName("model"); ArrayList lst = new ArrayList(); foreach(XmlNode model in models) { lst.Add(new Model(int.Parse(model.ChildNodes[0].FirstChild.Value), model.ChildNodes[1].FirstChild.Value, int.Parse(model.ChildNodes[2].FirstChild.Value))); } Console.WriteLine("som indlæst:"); for(int i = 0; i < lst.Count; i++) { Console.WriteLine(lst[i]); } lst.Sort(new ModelNameComparer()); Console.WriteLine("efter navne sortering:"); for(int i = 0; i < lst.Count; i++) { Console.WriteLine(lst[i]); } lst.Sort(new ModelYearComparer()); Console.WriteLine("efter årgangs ortering:"); for(int i = 0; i < lst.Count; i++) { Console.WriteLine(lst[i]); } } } }