Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am facing an issue while serialization and deserialzation of an xml string.
My function gets xml as a string and it should return and object. the existing code is
C#
MemoryStream oMemoryStream = null;
object oRootData = null;
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
Byte[] Bytes = oUTF8Encoding.GetByte(sXml);                                         
oMemoryStream = new MemoryStream(Bytes);               
oRootData = oXmlSerializer.Deserialize(oMemoryStream);


It was working fine till the time i have added a new field (sector_id) in my XSD file. Now the sXml contains the new attribute and value as per the XSD but when it deseialize the value for attribute sector_id becomes 0.

Can any one of please help me out of this ?
Posted

Sounds like the potential issue is that the code doing the serialising does not know about the new attribute. Are they all in the same project ?
 
Share this answer
 
Comments
[no name] 22-Aug-12 6:07am    
Yes, Everything is in the same project. oType is Type of a class having members {Id,Name,Parent,SectorId}. This class is in different project but referenced very well. I wanted to attach few snapshot but cant see any option here.

Did i answered your question or I missed it ?
Christian Graus 22-Aug-12 6:11am    
You can't attach images. So the code to save and load objects is in the same project, the object is in a different one ? If it were me, I'd create a new project, that imports this dll, and just serialises it back and forth. If that works, then you know there's an issue in your existing project. If the XML contains the value and it's not coming out, that suggests that this code cannot see that this property belongs to the class, which means that oType is the old type.
[no name] 22-Aug-12 6:22am    
I created a blank console application project. Referenced by oType class in that and tried to do teh same. Still it converts values to 0. Any Suggestions to check this ? I have tried Clean/Build/Rebuild.
Christian Graus 22-Aug-12 6:25am    
This proves that the issue is outside of the code you posted. So, to recap, the XML contains the value, but the reading code does not read the value in ? Why do you need to serialise as XML ? Have you tried serialising as a binary object ? If that works, then your XML serialisation code is the issue, if it doesn't, then the issue is probably deeper down.
[no name] 22-Aug-12 6:50am    
Why I need to serialise ? : No clue about that, Its an old existing code without comment. But the return of this method is a parameter of object type to another method. I am not sure about the Binary serialising and will check and come back to you.
I was using
[System.Xml.Serialization.XmlIgnoreAttribute()]
instead of
[System.Xml.Serialization.XmlAttributeAttribute()]
so the particular attribute was ignored by complier at run time for serialization.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900