Serialization problem

Serialization problem

am 19.12.2007 20:39:04 von 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:
5.25

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


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

Thaks for help!

RE: Serialization problem

am 20.12.2007 02:02:02 von unknown

"Art" wrote:

> - 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:
> 5.25
>
> But DB loaded version:
> Order.Commission = -5.23; // Commision is of type decimal
> Serializes to:
>
>
> 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.
--
John Saunders | MVP - Windows Server System - Connected System Developer |
https://mvp.support.microsoft.com/profile/John.Saunders

RE: Serialization problem

am 20.12.2007 15:57:00 von 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 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
}

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.

RE: Serialization problem

am 20.12.2007 17:58:00 von unknown

"Art" wrote:

> 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)?