By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Element order on serialization

P: n/a
Here's a class I'm working with
public class BatchHeader {
public string ScaleSite;
public string FromScaleDate;
public string ToScaleDate;
public string SubmitterBatchID;
System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string DocumentCount;
[System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string BatchControlTotal;

public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate ="second";

ToScaleDate = "third";

SubmitterBatchID = "4";

DocumentCount = "5";

BatchControlTotal = "sixth";

}

When I serialize it the resulting XML Doesn't keep the elements in any sort
of order that I can see I get back

<SubmitterBatchID> 4</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>six</BatchControlTotal>

<ScaleSite>first</ScaleSite>

<FromScaleDate>second</FromScaleDate>

<ToScaleDate>third</ToScaleDate>

This fails when I validate the file against the schema and since this is
just one of the classes and there are lots of them It makes the over all
resulting file impossible to read. How can I specify an order for the
resulting fields?
Mar 24 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

Your code looks a bit strange. How are you serializing this?
Since you don't have a default constructor, I can't understand
how you are able to use the XmlSerializer (it requires a default
constructor).

XmlSerializer takes all fields in the order that they are declared.

Sample code (assuming you have a default constructor on BatchHeader:

XmlSerializer xs = new XmlSerializer(typeof(BatchHeader));
StreamWriter sw = new StreamWriter("output.xml");
xs.Serialize(sw, new BatchHeader(new ArrayList()));

output.xml contains:
<BatchHeader xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ScaleSite>First</ScaleSite>
<FromScaleDate>second</FromScaleDate>
<ToScaleDate>third</ToScaleDate>
<SubmitterBatchID>4</SubmitterBatchID>
<DocumentCount>5</DocumentCount>
<BatchControlTotal>sixth</BatchControlTotal>
</BatchHeader>
Hope this helps!

regards
Emil Kvarnhammar
"jamie" <st*********@nospam.nospam> skrev i meddelandet
news:uo**************@TK2MSFTNGP11.phx.gbl...
Here's a class I'm working with
public class BatchHeader {
public string ScaleSite;
public string FromScaleDate;
public string ToScaleDate;
public string SubmitterBatchID;
System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string DocumentCount;
[System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string BatchControlTotal;

public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate ="second";

ToScaleDate = "third";

SubmitterBatchID = "4";

DocumentCount = "5";

BatchControlTotal = "sixth";

}

When I serialize it the resulting XML Doesn't keep the elements in any
sort of order that I can see I get back

<SubmitterBatchID> 4</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>six</BatchControlTotal>

<ScaleSite>first</ScaleSite>

<FromScaleDate>second</FromScaleDate>

<ToScaleDate>third</ToScaleDate>

This fails when I validate the file against the schema and since this is
just one of the classes and there are lots of them It makes the over all
resulting file impossible to read. How can I specify an order for the
resulting fields?

Mar 25 '06 #2

P: n/a
I didn't cut and paste all of the code as it's a lot which is why the
default constructor is missing. The default constructor is just
public BatchHeader()

{}

My serializer code is

HBS document = new HBS(selectedLoads); // selected loads is the
arraylist
XmlSerializer x = new XmlSerializer(typeof(HBS));

TextWriter writer = new
StreamWriter(Utilities.getWorkingDirectory()+"HBS. xml");

x.Serialize(writer, document);

So pretty much the same thing.

The HBS class calls the batch header class and numerous other classes.
Everything is out of order for whatever reason. This is done using the
compact framework serializer which I have noticed works a tiny bit
differently. If you don't have a default constructor it doesn't throw an
error for instance. SHould I move this to the Compact frame work forum?

"Emil Kvarnhammar" <info at ynax.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

Your code looks a bit strange. How are you serializing this?
Since you don't have a default constructor, I can't understand
how you are able to use the XmlSerializer (it requires a default
constructor).

XmlSerializer takes all fields in the order that they are declared.

Sample code (assuming you have a default constructor on BatchHeader:

XmlSerializer xs = new XmlSerializer(typeof(BatchHeader));
StreamWriter sw = new StreamWriter("output.xml");
xs.Serialize(sw, new BatchHeader(new ArrayList()));

output.xml contains:
<BatchHeader xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ScaleSite>First</ScaleSite>
<FromScaleDate>second</FromScaleDate>
<ToScaleDate>third</ToScaleDate>
<SubmitterBatchID>4</SubmitterBatchID>
<DocumentCount>5</DocumentCount>
<BatchControlTotal>sixth</BatchControlTotal>
</BatchHeader>
Hope this helps!

regards
Emil Kvarnhammar
"jamie" <st*********@nospam.nospam> skrev i meddelandet
news:uo**************@TK2MSFTNGP11.phx.gbl...
Here's a class I'm working with
public class BatchHeader {
public string ScaleSite;
public string FromScaleDate;
public string ToScaleDate;
public string SubmitterBatchID;
System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string DocumentCount;
[System.Xml.Serialization.XmlElementAttribute(DataT ype="integer")]

public string BatchControlTotal;

public BatchHeader(ArrayList batchLoads)

{

string[] BatchInfo;

ScaleSite = "First";

FromScaleDate ="second";

ToScaleDate = "third";

SubmitterBatchID = "4";

DocumentCount = "5";

BatchControlTotal = "sixth";

}

When I serialize it the resulting XML Doesn't keep the elements in any
sort of order that I can see I get back

<SubmitterBatchID> 4</SubmitterBatchID>

<DocumentCount>5</DocumentCount>

<BatchControlTotal>six</BatchControlTotal>

<ScaleSite>first</ScaleSite>

<FromScaleDate>second</FromScaleDate>

<ToScaleDate>third</ToScaleDate>

This fails when I validate the file against the schema and since this is
just one of the classes and there are lots of them It makes the over all
resulting file impossible to read. How can I specify an order for the
resulting fields?


Mar 27 '06 #3

P: n/a
Hi Jamie,

I'm using the same code you have used. And I get the xml file in the
correct order.

<?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">
<ScaleSite>First</ScaleSite>
<FromScaleDate>second</FromScaleDate>
<ToScaleDate>third</ToScaleDate>
<SubmitterBatchID>4</SubmitterBatchID>
<DocumentCount>5</DocumentCount>
<BatchControlTotal>sixth</BatchControlTotal>
</BatchHeader>

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

Mar 29 '06 #4

P: n/a
OK I've now entered reentered the code and I still get the error. Here is
the 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(DataT ype = "integer")]

public string DocumentCount;

[System.Xml.Serialization.XmlElementAttribute(DataT ype = "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.

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:33****************@TK2MSFTNGXA01.phx.gbl...
Hi Jamie,

I'm using the same code you have used. And I get the xml file in the
correct order.

<?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">
<ScaleSite>First</ScaleSite>
<FromScaleDate>second</FromScaleDate>
<ToScaleDate>third</ToScaleDate>
<SubmitterBatchID>4</SubmitterBatchID>
<DocumentCount>5</DocumentCount>
<BatchControlTotal>sixth</BatchControlTotal>
</BatchHeader>

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

Apr 7 '06 #5

P: n/a
OK having entered my code into a windows application It does work there so
it's definitely a CF issue as I see it.

"jamie" <st*********@nospam.nospam> wrote in message
news:Om**************@TK2MSFTNGP04.phx.gbl...
OK I've now entered reentered the code and I still get the error. Here
is the 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(DataT ype = "integer")]

public string DocumentCount;

[System.Xml.Serialization.XmlElementAttribute(DataT ype = "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.

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:33****************@TK2MSFTNGXA01.phx.gbl...
Hi Jamie,

I'm using the same code you have used. And I get the xml file in the
correct order.

<?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">
<ScaleSite>First</ScaleSite>
<FromScaleDate>second</FromScaleDate>
<ToScaleDate>third</ToScaleDate>
<SubmitterBatchID>4</SubmitterBatchID>
<DocumentCount>5</DocumentCount>
<BatchControlTotal>sixth</BatchControlTotal>
</BatchHeader>

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


Apr 7 '06 #6

P: n/a
Hi Jamie,

Sorry that I didn't notice that you got this problem on the Compact
Framework.

Unfortunately, this is by Design.

The order of elements serialized by the NETCF xml serializer is not
guaranteed to match that of the desktop. There is nothing in the generated
schema class included in the attached project that specifies the order of
the elements.

In order to accomplish this you should add the /order option to xsd.exe and
then regenerate the schema class
(xsd.exe /order /c foo.xsd)

By doing this all the particle members will have their explicit order
identifiers and then the serializer will honor the order of the schema. The
new schema generated by with the /order switch will have the orders
property specified on the XmlElementAttribute
e.g.
[System.Xml.Serialization.XmlElementAttribute(Order = 2)]

Please check the following link for more information

http://lab.msdn.microsoft.com/Produc...x?feedbackid=d
56d7e3f-14dc-484d-9aa8-f2e5b75c17ad

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

Apr 10 '06 #7

P: n/a
Well thats really annoying. Your fix did work nicely though. Thanks.
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:Fa**************@TK2MSFTNGXA01.phx.gbl...
Hi Jamie,

Sorry that I didn't notice that you got this problem on the Compact
Framework.

Unfortunately, this is by Design.

The order of elements serialized by the NETCF xml serializer is not
guaranteed to match that of the desktop. There is nothing in the generated
schema class included in the attached project that specifies the order of
the elements.

In order to accomplish this you should add the /order option to xsd.exe
and
then regenerate the schema class
(xsd.exe /order /c foo.xsd)

By doing this all the particle members will have their explicit order
identifiers and then the serializer will honor the order of the schema.
The
new schema generated by with the /order switch will have the orders
property specified on the XmlElementAttribute
e.g.
[System.Xml.Serialization.XmlElementAttribute(Order = 2)]

Please check the following link for more information

http://lab.msdn.microsoft.com/Produc...x?feedbackid=d
56d7e3f-14dc-484d-9aa8-f2e5b75c17ad

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

Apr 10 '06 #8

P: n/a
You're welcome, Jamie.

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

Apr 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.