473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Element order on serialization

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
8 2448
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
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
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
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
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
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
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
You're welcome, Jamie.

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

Apr 11 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lars | last post by:
Hello All, I have a question: is it possible to set the order of the elements of serialized object? Lets say we have Public Class Order Public Price As Decimal Private Quantity As Decimal...
6
by: jamie | last post by:
I'm serializing a class and some of the elements end up with blank results. This is fine. When I serialize the class the elements show up in the resulting XML as something like <SigningLicence/>...
3
by: namewitheldbyrequest | last post by:
"The XML element 'EnableTheming' from namespace 'http://tempuri.org/' is already present in the current scope" I created a Web Service: I imported System.Data.SqlClient so I could access SQL...
0
by: rval | last post by:
I have this schema while teh following sequence <complexType name="SetOfTasks" mixed="true"> <sequence> <choice minOccurs="1" maxOccurs="unbounded"> <element minOccurs="0"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.