XML Serialization Weird Exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was testing XML Serialization to assess if it fits my needs. I coded two
methods, one dumping the XML serialized object to a file, and the other that
recreates the object:

static Graph Awake(string filename)
{
System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(Graph));
System.IO.TextReader r = new System.IO.StreamReader(filename);

object res = s.Deserialize(r);
r.Close();

return (Graph) res;
}

static void Hibernate(string filename, Graph graph)
{
System.Xml.Serialization.XmlSerializer s = new
System.Xml.Serialization.XmlSerializer(typeof(Graph));
System.IO.TextWriter w = new System.IO.StreamWriter(filename, false);

s.Serialize(w, graph);

w.Close();
}


If I run those methods separately, they work fine.

However, if I run Hibernate and then Awake, Awake throws an exception...
Furthermore, if I command Hibernate to dump the XML data to a file other
than the one Awake is going to read from, Awake fails again with the same
error.

Any ideas? Bug?

Regards,
Manuel.
 
Could it be because the the reader isn't garbage collected yet and
still holds a reference to the file?

Try to put a w = null after you close the writer and perhaps a
GC.Collect() as well. Or you could open the file with sharing set to
FileShare.Read.

Regards,
HakonB
 
:(

I forgot to mention I did that. Actually, I have tried lots of hacks to make
this work.
Besides, regardless whether the writer is collected or not, I closed it, so
it should have clear any references to the file/stream.

Thanks,
Manuel.
 
This the message:

There is an error in XML document (4, 16).

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at CPN_WF_Test.Class1.Awake(String filename) in
d:\users\manu\programming\tes
ts\cpn wf test\class1.cs:line 17
at CPN_WF_Test.Class1.TestAwake() in d:\users\manu\programming\tests\cpn
wf t
est\class1.cs:line 44
at CPN_WF_Test.Class1.Main(String[] args) in
d:\users\manu\programming\tests\
cpn wf test\class1.cs:line 124
 
Hello Manuel,

Have you verified that a complete document is in fact saved when you call
Hibernate? Between Hibernate and Awake, check and make sure that the complete
file is written out (Im guessing its not).

You might try something along this line:

static void Hibernate(string filename, Graph graph)
{
XmlSerializer s = new XmlSerializer(typeof(Graph));
TextWriter writer = new StreamWriter(filename, false);
s.Serialize(writer, graph);

writer.Flush();
writer.Close();
}

The trick is probably in the Flush command. I cant count how many times Ive
been bitten by that.

--
Matt Berther
http://www.mattberther.com
This the message:

There is an error in XML document (4, 16).

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at CPN_WF_Test.Class1.Awake(String filename) in
d:\users\manu\programming\tes
ts\cpn wf test\class1.cs:line 17
at CPN_WF_Test.Class1.TestAwake() in
d:\users\manu\programming\tests\cpn
wf t
est\class1.cs:line 44
at CPN_WF_Test.Class1.Main(String[] args) in
d:\users\manu\programming\tests\
cpn wf test\class1.cs:line 124

HakonB said:
Hello Manuel,

Which exception is thrown?

Regards,
HakonB
 
I have verified it.
The flush trick didn't make it.

I think this is a BUG. Do you?

Manuel.

Matt Berther said:
Hello Manuel,

Have you verified that a complete document is in fact saved when you call
Hibernate? Between Hibernate and Awake, check and make sure that the complete
file is written out (Im guessing its not).

You might try something along this line:

static void Hibernate(string filename, Graph graph)
{
XmlSerializer s = new XmlSerializer(typeof(Graph));
TextWriter writer = new StreamWriter(filename, false);
s.Serialize(writer, graph);

writer.Flush();
writer.Close();
}

The trick is probably in the Flush command. I cant count how many times Ive
been bitten by that.

--
Matt Berther
http://www.mattberther.com
This the message:

There is an error in XML document (4, 16).

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at CPN_WF_Test.Class1.Awake(String filename) in
d:\users\manu\programming\tes
ts\cpn wf test\class1.cs:line 17
at CPN_WF_Test.Class1.TestAwake() in
d:\users\manu\programming\tests\cpn
wf t
est\class1.cs:line 44
at CPN_WF_Test.Class1.Main(String[] args) in
d:\users\manu\programming\tests\
cpn wf test\class1.cs:line 124

HakonB said:
Hello Manuel,

Which exception is thrown?

Regards,
HakonB
 
Hello Manuel,

I would propose that you create the smallest duplicatable example of this
issue and attach all relevant code to your reply to this message. Id be glad
to look at it more with you.

I can not get the error that you're seeing.

--
Matt Berther
http://www.mattberther.com
I have verified it.
The flush trick didn't make it.
I think this is a BUG. Do you?

Manuel.

Matt Berther said:
Hello Manuel,

Have you verified that a complete document is in fact saved when you
call Hibernate? Between Hibernate and Awake, check and make sure that
the complete file is written out (Im guessing its not).

You might try something along this line:

static void Hibernate(string filename, Graph graph)
{
XmlSerializer s = new XmlSerializer(typeof(Graph));
TextWriter writer = new StreamWriter(filename, false);
s.Serialize(writer, graph);
writer.Flush();
writer.Close();
}
The trick is probably in the Flush command. I cant count how many
times Ive been bitten by that.

--
Matt Berther
http://www.mattberther.com
This the message:

There is an error in XML document (4, 16).

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at CPN_WF_Test.Class1.Awake(String filename) in
d:\users\manu\programming\tes
ts\cpn wf test\class1.cs:line 17
at CPN_WF_Test.Class1.TestAwake() in
d:\users\manu\programming\tests\cpn
wf t
est\class1.cs:line 44
at CPN_WF_Test.Class1.Main(String[] args) in
d:\users\manu\programming\tests\
cpn wf test\class1.cs:line 124
:

Hello Manuel,

Which exception is thrown?

Regards,
HakonB
 
Mea culpa est.
I found the bug. It's all mine, and I'm ashame having bothered you this long.
It was a global (static) registration scheme that was cleaned after the
Graph object was collected.

I added support for IDisposable interface, called the Dispose method, and
all worked fine.

Sorry,
Manuel.

Matt Berther said:
Hello Manuel,

I would propose that you create the smallest duplicatable example of this
issue and attach all relevant code to your reply to this message. Id be glad
to look at it more with you.

I can not get the error that you're seeing.

--
Matt Berther
http://www.mattberther.com
I have verified it.
The flush trick didn't make it.
I think this is a BUG. Do you?

Manuel.

Matt Berther said:
Hello Manuel,

Have you verified that a complete document is in fact saved when you
call Hibernate? Between Hibernate and Awake, check and make sure that
the complete file is written out (Im guessing its not).

You might try something along this line:

static void Hibernate(string filename, Graph graph)
{
XmlSerializer s = new XmlSerializer(typeof(Graph));
TextWriter writer = new StreamWriter(filename, false);
s.Serialize(writer, graph);
writer.Flush();
writer.Close();
}
The trick is probably in the Flush command. I cant count how many
times Ive been bitten by that.

--
Matt Berther
http://www.mattberther.com
This the message:

There is an error in XML document (4, 16).

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, St
ring encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader
textReader)
at CPN_WF_Test.Class1.Awake(String filename) in
d:\users\manu\programming\tes
ts\cpn wf test\class1.cs:line 17
at CPN_WF_Test.Class1.TestAwake() in
d:\users\manu\programming\tests\cpn
wf t
est\class1.cs:line 44
at CPN_WF_Test.Class1.Main(String[] args) in
d:\users\manu\programming\tests\
cpn wf test\class1.cs:line 124
:

Hello Manuel,

Which exception is thrown?

Regards,
HakonB
 
Back
Top