Serialization problem

A

Art

- I populate [deserialize] order class from a file (order.xml)- works OK,
- Serialize back to a file - works OK,

Now ...

- I populate another instance of order class with data from DB,
- at this time both classes, deserialized and populated form DB, are identical
- when I serialize DB version one field always gets omitted

So, deserialized version:
Order.Commission = -5.23; // Commision is of type decimal
Serializes to:
<Order><Commission>5.25</Commission></Order>

But DB loaded version:
Order.Commission = -5.23; // Commision is of type decimal
Serializes to:
<Order></Order>

Both use the same Serialize function. What can be wrong?
BTW, when I rename Commission to, say, Commission2 it works. WTF..

Thaks for help!
 
J

John Saunders

Art said:
- I populate [deserialize] order class from a file (order.xml)- works OK,
- Serialize back to a file - works OK,

Now ...

- I populate another instance of order class with data from DB,
- at this time both classes, deserialized and populated form DB, are identical
- when I serialize DB version one field always gets omitted

So, deserialized version:
Order.Commission = -5.23; // Commision is of type decimal
Serializes to:
<Order><Commission>5.25</Commission></Order>

But DB loaded version:
Order.Commission = -5.23; // Commision is of type decimal
Serializes to:
<Order></Order>

Both use the same Serialize function. What can be wrong?
BTW, when I rename Commission to, say, Commission2 it works. WTF..

Please post some code. I'm unable to guess what's wrong from your description.
 
A

Art

John,
Here it is:

private void FromFile()
{
// deserialize
StreamReader sr = new StreamReader(@"..\..\order.xml");
TextReader reader = new StringReader(sr.ReadToEnd());
XmlSerializer ser = new XmlSerializer(typeof(Order));
Order order = (Order)ser.Deserialize(reader);
reader.Close();
sr.Dispose();

//serialize
XmlDocument xmldoc = Serialize(order);
Debug.WriteLine(xmldoc.OuterXml);
// xmldoc contains <Commission> node with 4.55 value
}

private void FromDB()
{
OrderRequest or = new OrderRequest();
Order order = or.getOrder(30002051);
// yes, at this time order.commission = 4.55
XmlDocument xmldoc = Serialize(order);
Debug.WriteLine(xmldoc.OuterXml);
// xmldoc contains all nodes except <Commission>
}

private XmlDocument Serialize(Order order)
{
XmlSerializer ser = new XmlSerializer(typeof(Order));
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, order);
ms.Seek(0, 0);
XmlDocument doc = new XmlDocument();
doc.Load(ms);
return doc;
}

Both procedures use the same Serialize() function and yes, when order is
returned from or.getOrder(30002051) the Commission property is set to 4.55,
exactly as expected, just for some reason does not get serialized.
Thanks for your help.
 
J

John Saunders [MVP]

Art said:
John,
Here it is:
....

Both procedures use the same Serialize() function and yes, when order is
returned from or.getOrder(30002051) the Commission property is set to 4.55,
exactly as expected, just for some reason does not get serialized.

Could you post the Order class, or at least the class declaration (with
attributes) and the Commission property (with attributes)?
 

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