Problem while Deserializing [Solved]
-
hi all, i am trying to deserialize a xml string into an object but i am getting following error : "An exception of type 'System.InvalidCastOperationException' occured in System.Xml.dll but was not handled in user code. Additional information: There is an error in XML document(1,2)." I am using following code to deserialize :
object obj = new object();
System.Xml.Serialization.XmlSerializer x = new XmlSerializer(obj.GetType());
StringReader sr = new StringReader(strNewXml);// strNewXml is a string in xml format
XmlTextReader xml = new XmlTextReader(sr);
obj = (object)x.Deserialize(xml);//Here i am getting errorI tried to see the value of XmlTextReader xml, but it is showing as {None}. I even tried with a string which i earlier serialized from an object but same error is displaying with it too. Can anyone tell me where i am doing wrong. Thanks and Regards, Nagendra
-
hi all, i am trying to deserialize a xml string into an object but i am getting following error : "An exception of type 'System.InvalidCastOperationException' occured in System.Xml.dll but was not handled in user code. Additional information: There is an error in XML document(1,2)." I am using following code to deserialize :
object obj = new object();
System.Xml.Serialization.XmlSerializer x = new XmlSerializer(obj.GetType());
StringReader sr = new StringReader(strNewXml);// strNewXml is a string in xml format
XmlTextReader xml = new XmlTextReader(sr);
obj = (object)x.Deserialize(xml);//Here i am getting errorI tried to see the value of XmlTextReader xml, but it is showing as {None}. I even tried with a string which i earlier serialized from an object but same error is displaying with it too. Can anyone tell me where i am doing wrong. Thanks and Regards, Nagendra
I have solved the problem by modifying my code as below:
object objNewTransactionEntry = new object();
XmlRootAttribute xmlRoot = new XmlRootAttribute("Session");
System.Xml.Serialization.XmlSerializer y = new XmlSerializer(objNewTransactionEntry.GetType(), xmlRoot);
StringReader sReader = new StringReader(strNewXml);
XmlTextReader xmlReader = new XmlTextReader(sReader);
objNewTransactionEntry = (object)y.Deserialize(xmlReader);