HighTechTalks DotNet Forums  

Serialization problem

Dotnet General Discussions microsoft.public.dotnet.general


Discuss Serialization problem in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Art
 
Posts: n/a

Default Serialization problem - 12-19-2007 , 02:39 PM






- 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!

Reply With Quote
  #2  
Old   
John Saunders
 
Posts: n/a

Default RE: Serialization problem - 12-19-2007 , 08:02 PM






"Art" wrote:

Quote:
- 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.
--
John Saunders | MVP - Windows Server System - Connected System Developer |
https://mvp.support.microsoft.com/profile/John.Saunders



Reply With Quote
  #3  
Old   
Art
 
Posts: n/a

Default RE: Serialization problem - 12-20-2007 , 09:57 AM



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.







Reply With Quote
  #4  
Old   
John Saunders [MVP]
 
Posts: n/a

Default RE: Serialization problem - 12-20-2007 , 11:58 AM



"Art" wrote:

Quote:
John,
Here it is:

....

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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.