XML serialization and item order

J

jamie

I've posted this in the XML forum and basically been told that it works for
them so I'm thinking this is A CF issue.

Basically I'm trying to serialize a class and the elements are coming back
out of order. Order is important in this case for me.

Here is my code

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.Schema;

using System.Xml.Serialization;

using System.IO;

using System.Text;

namespace XML_test1

{

public partial class Form1 : Form

{

public Form1()

{

ArrayList temp = new ArrayList();

temp.Add("1");

BatchHeader test = new BatchHeader(temp);

InitializeComponent();

try

{

XmlSerializer x = new XmlSerializer(typeof(BatchHeader));

TextWriter writer = new StreamWriter("\\Program Files\\log scale manager
2\\" + "HBS.xml");

x.Serialize(writer, test);



MessageBox.Show("Done");

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

private void button1_Click(object sender, EventArgs e)

{

this.Close();

}

}

public class BatchHeader

{

public string ScaleSite;

public string FromScaleDate;

public string ToScaleDate;

public string SubmitterBatchID;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string DocumentCount;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string BatchControlTotal;


public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate = "Second";

ToScaleDate = "Third";

SubmitterBatchID = "fourth";

DocumentCount = "5";

BatchControlTotal = "6";

}

// default constructor needed for serializing

public BatchHeader()

{

}

}

}



And here is the result

<?xml version="1.0" encoding="utf-8"?>

<BatchHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

</BatchHeader>

This is all created in VS 2005, testing is being done on a Pocket PC
device. It's the same if I use the pocket pc 2003 emulator.

I would like it to come back

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

but I can't figure out what is affecting the ordering in the results.

I have noticed that If I have a value of a space that becomes the first
element but I don't know if that's all the time or not.

Anyone have any ideas?
 
J

jamie

OK I've entered the code into a windows application and sure enough it works
there so this appears to be a CF issue.

jamie said:
I've posted this in the XML forum and basically been told that it works
for them so I'm thinking this is A CF issue.

Basically I'm trying to serialize a class and the elements are coming back
out of order. Order is important in this case for me.

Here is my code

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.Schema;

using System.Xml.Serialization;

using System.IO;

using System.Text;

namespace XML_test1

{

public partial class Form1 : Form

{

public Form1()

{

ArrayList temp = new ArrayList();

temp.Add("1");

BatchHeader test = new BatchHeader(temp);

InitializeComponent();

try

{

XmlSerializer x = new XmlSerializer(typeof(BatchHeader));

TextWriter writer = new StreamWriter("\\Program Files\\log scale manager
2\\" + "HBS.xml");

x.Serialize(writer, test);



MessageBox.Show("Done");

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

private void button1_Click(object sender, EventArgs e)

{

this.Close();

}

}

public class BatchHeader

{

public string ScaleSite;

public string FromScaleDate;

public string ToScaleDate;

public string SubmitterBatchID;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string DocumentCount;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string BatchControlTotal;


public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate = "Second";

ToScaleDate = "Third";

SubmitterBatchID = "fourth";

DocumentCount = "5";

BatchControlTotal = "6";

}

// default constructor needed for serializing

public BatchHeader()

{

}

}

}



And here is the result

<?xml version="1.0" encoding="utf-8"?>

<BatchHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

</BatchHeader>

This is all created in VS 2005, testing is being done on a Pocket PC
device. It's the same if I use the pocket pc 2003 emulator.

I would like it to come back

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

but I can't figure out what is affecting the ordering in the results.

I have noticed that If I have a value of a space that becomes the first
element but I don't know if that's all the time or not.

Anyone have any ideas?
 
I

Ilya Tumanov [MS]

Order of elements in XML serialization is not guaranteed. In case of NETCF
order would be determined by order in which properties are returned by
reflection APIs.

If you need specific order, please implement IXmlSerializable interface and
do serialization/deserialization manually in order you need.


--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).

jamie said:
I've posted this in the XML forum and basically been told that it works
for them so I'm thinking this is A CF issue.

Basically I'm trying to serialize a class and the elements are coming back
out of order. Order is important in this case for me.

Here is my code

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.Schema;

using System.Xml.Serialization;

using System.IO;

using System.Text;

namespace XML_test1

{

public partial class Form1 : Form

{

public Form1()

{

ArrayList temp = new ArrayList();

temp.Add("1");

BatchHeader test = new BatchHeader(temp);

InitializeComponent();

try

{

XmlSerializer x = new XmlSerializer(typeof(BatchHeader));

TextWriter writer = new StreamWriter("\\Program Files\\log scale manager
2\\" + "HBS.xml");

x.Serialize(writer, test);



MessageBox.Show("Done");

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

private void button1_Click(object sender, EventArgs e)

{

this.Close();

}

}

public class BatchHeader

{

public string ScaleSite;

public string FromScaleDate;

public string ToScaleDate;

public string SubmitterBatchID;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string DocumentCount;

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]

public string BatchControlTotal;


public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate = "Second";

ToScaleDate = "Third";

SubmitterBatchID = "fourth";

DocumentCount = "5";

BatchControlTotal = "6";

}

// default constructor needed for serializing

public BatchHeader()

{

}

}

}



And here is the result

<?xml version="1.0" encoding="utf-8"?>

<BatchHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

</BatchHeader>

This is all created in VS 2005, testing is being done on a Pocket PC
device. It's the same if I use the pocket pc 2003 emulator.

I would like it to come back

<ScaleSite>First</ScaleSite>

<FromScaleDate>Second</FromScaleDate>

<ToScaleDate>Third</ToScaleDate>

SubmitterBatchID>fourth</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>6</BatchControlTotal>

but I can't figure out what is affecting the ordering in the results.

I have noticed that If I have a value of a space that becomes the first
element but I don't know if that's all the time or not.

Anyone have any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top