2

In this post, I could get an XML file generated based on C# class.

Can I reorder the XML elements based on its element? My code uses

var ser = new XmlSerializer(typeof(Module));
ser.Serialize(WriteFileStream, report, ns);
WriteFileStream.Close();

to get the XML file, but I need to have the XML file sorted based on a BlocksCovered variable.

public class ClassInfo {
    public string ClassName;
    public int BlocksCovered;
    public int BlocksNotCovered;
    public double CoverageRate;

    public ClassInfo() {}

    public ClassInfo(string ClassName, int BlocksCovered, int BlocksNotCovered, double CoverageRate) 
    {
        this.ClassName = ClassName;
        this.BlocksCovered = BlocksCovered;
        this.BlocksNotCovered = BlocksNotCovered;
        this.CoverageRate = CoverageRate;
    }
}

[XmlRoot("Module")]
public class Module {
    [XmlElement("Class")]
    public List<ClassInfo> ClassInfoList;
    public int BlocksCovered;
    public int BlocksNotCovered;
    public string moduleName;

    public Module()
    {
        ClassInfoList = new List<ClassInfo>();
        BlocksCovered = 0;
        BlocksNotCovered = 0;
        moduleName = "";
    }
}

Module report = new Module();

...

TextWriter WriteFileStream = new StreamWriter(xmlFileName);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
var ser = new XmlSerializer(typeof(Module));
ser.Serialize(WriteFileStream, report, ns);
WriteFileStream.Close();

SOLVED

public void Sort()
{
    ClassInfoList = ClassInfoList.OrderBy( x=>x.CoverageRate).ToList();
}
Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • if your `ClassInfoList` is sorted already (as your previous question implies it is now), is the output XML produced by the `XmlSerializer` not sorted? I would be surprised – BrokenGlass Mar 07 '11 at 23:57
  • @BrokenGlass : The ClassInfoList not sorted at all, I need to get a sorted output from unsorted ClassInfoList. – prosseek Mar 07 '11 at 23:59
  • well - why don't you sort it before serializing? That would be the easiest solution. – BrokenGlass Mar 08 '11 at 00:05
  • possible duplicate of [Any way to make XmlSerializer output xml in a defined order?](http://stackoverflow.com/questions/612160/any-way-to-make-xmlserializer-output-xml-in-a-defined-order) – Jon Mar 08 '11 at 00:06
  • @Jon: not a duplicate OP wants the contents of a list element to be sorted, not the elements of his class – BrokenGlass Mar 08 '11 at 00:11
  • @Jon et. al. Not a duplicate at all. – John Saunders Mar 08 '11 at 00:53

1 Answers1

5

Just sort your list before serializing, it appears in your code that you have control over the serialization - if that is the case just sort the ClassInfoList list before serializing your Module by adding a Sort() method:

public class Module
{

  public void Sort()
  {
      ClassInfoList = ClassInfoList.OrderBy( x=>x.BlocksCovered).ToList();
  }
..
}

then call Sort() before serializing:

report.Sort();
ser.Serialize(WriteFileStream, report, ns);

If you don't have control over when/and how your class is serialized have your Module class implement IXmlSerializable and sort within your Serialize() method - the later is more effort though and should be avoided.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335