Home All Groups Group Topic Archive Search About

XML Serialization Weird Exception

Author
16 Feb 2005 2:15 PM
Manuel Vázquez

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.

Author
16 Feb 2005 3:09 PM
HakonB
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
Are all your drivers up to date? click for free checkup

Author
16 Feb 2005 3:45 PM
Manuel Vázquez
:(

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.

Show quoteHide quote
"HakonB" wrote:

> 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
>
>
Author
16 Feb 2005 3:57 PM
HakonB
Hello Manuel,

Which exception is thrown?

Regards,
HakonB

Show quoteHide quote
> :(
>
> 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.
> "HakonB" wrote:
>
>> 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
Author
17 Feb 2005 9:53 PM
Manuel Vázquez
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

Show quoteHide quote
"HakonB" wrote:

> Hello Manuel,
>
> Which exception is thrown?
>
> Regards,
> HakonB
>
Author
18 Feb 2005 1:42 AM
Matt Berther
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.

Show quoteHide quote
> 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" wrote:
>
>> Hello Manuel,
>>
>> Which exception is thrown?
>>
>> Regards,
>> HakonB
Author
18 Feb 2005 10:07 PM
Manuel Vázquez
I have verified it.
The flush trick didn't make it.

I think this is a BUG. Do you?

Manuel.

Show quoteHide quote
"Matt Berther" wrote:

> 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" wrote:
> >
> >> Hello Manuel,
> >>
> >> Which exception is thrown?
> >>
> >> Regards,
> >> HakonB
>
>
>
>
Author
19 Feb 2005 5:58 AM
Matt Berther
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.

Show quoteHide quote
> I have verified it.
> The flush trick didn't make it.
> I think this is a BUG. Do you?
>
> Manuel.
>
> "Matt Berther" wrote:
>
>> 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" wrote:
>>>
>>>> Hello Manuel,
>>>>
>>>> Which exception is thrown?
>>>>
>>>> Regards,
>>>> HakonB
Author
19 Feb 2005 9:31 PM
Manuel Vázquez
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.

Show quoteHide quote
"Matt Berther" wrote:

> 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" wrote:
> >
> >> 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" wrote:
> >>>
> >>>> Hello Manuel,
> >>>>
> >>>> Which exception is thrown?
> >>>>
> >>>> Regards,
> >>>> HakonB
>
>
>
>

Bookmark and Share