c# 4.0 - how to define the order of array element while serializing xml object in c# -


i have following class.

[xmlroot("myroot")] public class myroot {     [xmlelement("node1")]     public node1[] node1 { get; set; }     [xmlelement("node2")]     public node2[] node2 { get; set; }     [xmlelement("node3")]     public node3[] node3 { get; set; }  }  public class node1 {     [xmlelement("attrib11")]     public string attrib11 { get; set; }     [xmlelement("attrib12")]     public string attrib12 { get; set; } }  public class node2 {     [xmlelement("attrib21")]     public string attrib21 { get; set; }     [xmlelement("attrib22")]     public string attrib22 { get; set; } } public class node3 {     [xmlelement("attrib31")]     public string attrib31 { get; set; }     [xmlelement("attrib32")]     public string attrib32 { get; set; } } 

this below code fill data , serialize

var abc = new xml834.myroot(); abc.node1 = new xml834.node1[] { new xml834.node1() { attrib11 = "a11", attrib12 = "b12" }, new xml834.node1() { attrib11 = "c11", attrib12 = "c12" } }; abc.node2 = new xml834.node2[] { new xml834.node2() { attrib21 = "a21", attrib22 = "b22" }, new xml834.node2() { attrib21 = "c21", attrib22 = "c22" } }; abc.node3 = new xml834.node3[] { new xml834.node3() { attrib31 = "a31", attrib32 = "b32" }, new xml834.node3() { attrib31 = "c31", attrib32 = "c32" } };  string xmlstring = null; using (memorystream memorystream = new memorystream()) using (xmltextwriter xmltextwriter = new xmltextwriter(memorystream, encoding.utf8)) {     xmlserializer xs = new xmlserializer(typeof(xml834.myroot));     xs.serialize(xmltextwriter, abc);     memorystream memorybasestream;     memorybasestream = (memorystream)xmltextwriter.basestream;     utf8encoding encoding = new utf8encoding();     xmlstring = encoding.getstring(memorybasestream.toarray());     memorybasestream.dispose();     xmltextwriter.close();     memorystream.close();     console.writeline(xmlstring); } 

the output getting is

 <?xml version="1.0" encoding="utf 8" ?>  <myroot xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema instance"> <node1>   <attrib11>a11</attrib11>    <attrib12>b12</attrib12>  </node1> <node1>   <attrib11>c11</attrib11>    <attrib12>c12</attrib12>  </node1> <node2>   <attrib21>a21</attrib21>    <attrib22>b22</attrib22>  </node2> <node2>   <attrib21>c21</attrib21>    <attrib22>c22</attrib22>  </node2> <node3>   <attrib31>a31</attrib31>    <attrib32>b32</attrib32>  </node3> <node3>   <attrib31>c31</attrib31>    <attrib32>c32</attrib32>  </node3> </myroot> 

i trying specify order of array item. possible output?

 <?xml version="1.0" encoding="utf 8" ?>  <myroot xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema instance"> <node1>   <attrib11>a11</attrib11>    <attrib12>b12</attrib12>  </node1> <node2>   <attrib21>a21</attrib21>    <attrib22>b22</attrib22>  </node2> <node3>   <attrib31>c31</attrib31>    <attrib32>c32</attrib32>  </node3> <node1>   <attrib11>c11</attrib11>    <attrib12>c12</attrib12>  </node1> <node2>   <attrib21>c21</attrib21>    <attrib22>c22</attrib22>  </node2> <node3>   <attrib31>a31</attrib31>    <attrib32>b32</attrib32>  </node3> </myroot> 

i suggest using xelement , customize serialization this

public class wrapper {     [xmlelement("node1")]     public node1 node1 { get; set; }     [xmlelement("node2")]     public node2 node2 { get; set; }     [xmlelement("node3")]     public node3 node3 { get; set; } }   [xmlroot("myroot")] public class myroot {           private list<wrapper> _wrappers;            public myroot() { _wrappers = new list<wrapper>(); }     public list<wrapper> wrappers    {           { return _wrappers; }           set { _wrappers = value; }    }     public string serialize()    {        if (_wrappers.any())        {            xelement inner = new xelement("myroot");            foreach (var w in _wrappers)            {                 if (w.node1 != null)                    inner.add(  w.node1.toxelement<node1>() );                 if (w.node2 != null)                    inner.add(  w.node2.toxelement<node2>() );                if (w.node3 != null)                    inner.add( w.node3.toxelement<node3>() );            }            return inner.tostring();        }        return string.empty;    } } 

toxelement extension got here hope helps!


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -