I need to serialize to Xml then at a later time deserialize to one of my classes.
Using this to serialize:
and this to deserialize:Code:public void Serialize() // serialize Schedule to XML file { try { XmlSerializer xs = new XmlSerializer(typeof(MSTSSchedule)); using (StreamWriter writer = new StreamWriter("c:\\temp\\serialized.xml",true)) { xs.Serialize(writer, this); } } catch (Exception x) {
and this to test it all:Code:public MSTSSchedule DeSerialize(XmlDocument doc) { MSTSSchedule sch = new MSTSSchedule(); try { XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement); XmlSerializer ser = new XmlSerializer(typeof(MSTSSchedule)); object obj = ser.Deserialize(reader); sch = (MSTSSchedule)obj; sch.SetResponse(ResponseCodes.RESP_ACK); } catch (System.Exception ex) { sch.SetResponse(ResponseCodes.RESP_NAK); } return sch; }
I get the "[System.Xml.XmlException] = {"Data at the root level is invalid. Line 1, position 1."}" error.Code:try { schedToday.Serialize(); XmlDocument xd = new XmlDocument(); xd.LoadXml("c:\\temp\\serialized.xml"); MSTSSchedule test = schedToday.DeSerialize(xd); } catch (System.Exception ex) {
Xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<MSTSSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m_Schedule>
<MSTSAppointment>
My question - shouldn't I be able to deserialize a Xml document that the .net serializer serialized??
using .Net 3.5sp1




Reply With Quote