|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
XML Serialization Weird Exceptionmethods, 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. 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 > > 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 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 > 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 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 > > > > 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 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 > > > >
Other interesting topics
Posting using httpwebrequest
.NET Framework 1.1 Configuration won't launch Creating Collections in Classes Microsoft: Help with OLE documents... what is best practises for sending out emails from .net? dll inside a dll? Setup project of .Net Framework 1.1 and Mdac 2.7 Adding function to Excel scheduled windows task Memory leak ? iexplore.exe? |
|||||||||||||||||||||||