HighTechTalks DotNet Forums  

Deserialization Problems Using XmlWriter vs. StringReader

Dotnet XML microsoft.public.dotnet.xml


Discuss Deserialization Problems Using XmlWriter vs. StringReader in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
depalau@bemis.com
 
Posts: n/a

Default Deserialization Problems Using XmlWriter vs. StringReader - 06-14-2007 , 02:05 PM






I'm experiencing issues where XmlSerialier.Deserialize throws an
exception when attempting to use a MemoryStream built with an
XmlWriter vs. a standalone StringReader. It is attempting to
deserialize a complex object.

Here is what I have:

0. Environment: Visual Studio 2005, VB.Net. I won't go into the gory
details of my complex object, just suffice to say that I need to
return XML instead of the object directly from the web service. I'm
also not including all of the code around things (try..catches, etc.)

1. I've defined a object built by using the Xsd.exe tool against a
schema I created.

2. On the web service side, I serialize my object and return an
XmlDocument:


Dim myObject As MyClass
Dim serializer As XmlSerializer
Dim writer As XmlWriter
Dim stream As MemoryStream = New MemoryStream()
Dim document As XmlDocument

writer = XmlWriter.Create(stream)
serializer = New XmlSerializer(myObject.GetType())
serializer.Serialize(writer, myObject)
stream.Seek(0, SeekOrigin.Begin)

document = New XmlDocument()
document.Load(stream)

This works good - the Xml shows up as a valid (I think) document: I
see it in my browser correctly and can open it up in a variety of
other things (XML Notepad, Word, etc.) and have validated it against a
couple of on-line validators.

3. On the client side, I attempt to deserialize it:

Dim node As XmlNode
Dim serializer As XmlSerializer
Dim myObject As MyClass
Dim writer As XmlWriter
Dim stream As MemoryStream

node = myService.GetMyObject()

stream = New MemoryStream
writer = XmlWriter.Create(stream)
node.WriteTo(writer)
stream.Seek(0, SeekOrigin.Begin)

serializer = New XmlSerializer(myObject.GetType())
myObject - serializer.Deserialize(stream)


This last statement throws an InvalidOperationException with "There is
an error in XML document (1, 92143)". The InnerException is "There is
an unclosed literal string. Line 1, position 92143". I can't really
think of a good way to get to position 92143 to see what the issue is.

However, when I do the following:

Dim reader As StringReader = New StringReader(node.OuterXml)
myObject = serializer.Deserialize(reader)

....it works just fine.

I tried messing around with creating an XmlWriterSettings object and
changing the encoding to Unicode and turning of CheckCharacters, but
no dice.

Anything obviously wrong? If necessary I can add more depth to the
psuedocode above.

Thanks,

-David


Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Deserialization Problems Using XmlWriter vs. StringReader - 06-15-2007 , 07:31 AM






depalau (AT) bemis (DOT) com wrote:

Quote:
This last statement throws an InvalidOperationException with "There is
an error in XML document (1, 92143)". The InnerException is "There is
an unclosed literal string. Line 1, position 92143". I can't really
think of a good way to get to position 92143 to see what the issue is.

Quote:
I tried messing around with creating an XmlWriterSettings object and
changing the encoding to Unicode and turning of CheckCharacters, but
no dice.
Can you try to write out the XML with XmlWriterSettings and Indent set
to true? Then perhaps you get an error message with a line number and a
position which is easier to find than position 92143 in a single line.


Other than that I don't think you need to use the MemoryStream, you can
simply create an XmlNodeReader over your node e.g.
serializer = New XmlSerializer(myObjct.GetType())
myObject = serializer.Deserialize(New XmlNodeReader(node))
I don't know obviously if that way the problem vanishes but you could
try it and let us know.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Deserialization Problems Using XmlWriter vs. StringReader - 06-15-2007 , 07:31 AM



depalau (AT) bemis (DOT) com wrote:

Quote:
This last statement throws an InvalidOperationException with "There is
an error in XML document (1, 92143)". The InnerException is "There is
an unclosed literal string. Line 1, position 92143". I can't really
think of a good way to get to position 92143 to see what the issue is.

Quote:
I tried messing around with creating an XmlWriterSettings object and
changing the encoding to Unicode and turning of CheckCharacters, but
no dice.
Can you try to write out the XML with XmlWriterSettings and Indent set
to true? Then perhaps you get an error message with a line number and a
position which is easier to find than position 92143 in a single line.


Other than that I don't think you need to use the MemoryStream, you can
simply create an XmlNodeReader over your node e.g.
serializer = New XmlSerializer(myObjct.GetType())
myObject = serializer.Deserialize(New XmlNodeReader(node))
I don't know obviously if that way the problem vanishes but you could
try it and let us know.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
depalau@bemis.com
 
Posts: n/a

Default Re: Deserialization Problems Using XmlWriter vs. StringReader - 07-03-2007 , 05:31 PM



On Jun 15, 6:31 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
depa... (AT) bemis (DOT) com wrote:
This last statement throws an InvalidOperationException with "There is
an error in XML document (1, 92143)". The InnerException is "There is
an unclosed literal string. Line 1, position 92143". I can't really
think of a good way to get to position 92143 to see what the issue is.
I tried messing around with creating an XmlWriterSettings object and
changing the encoding to Unicode and turning of CheckCharacters, but
no dice.

Can you try to write out the XML with XmlWriterSettings and Indent set
to true? Then perhaps you get an error message with a line number and a
position which is easier to find than position 92143 in a single line.

Other than that I don't think you need to use the MemoryStream, you can
simply create an XmlNodeReader over your node e.g.
serializer = New XmlSerializer(myObjct.GetType())
myObject = serializer.Deserialize(New XmlNodeReader(node))
I don't know obviously if that way the problem vanishes but you could
try it and let us know.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
I tried changing the XmlWriter to indent, but was still unable to
determine what exactly was causing the issue - it was at a position
that seemed like valid XML to me. It was in the middle of the
document and the element above and below seemed okay as well.

But I did change the code to use a XmlNodeReader like you suggested
and it worked. So I'm not going to get too worked up about what could
potentially be wrong at this time. Thanks for the pointers.



Reply With Quote
  #5  
Old   
depalau@bemis.com
 
Posts: n/a

Default Re: Deserialization Problems Using XmlWriter vs. StringReader - 07-03-2007 , 05:31 PM



On Jun 15, 6:31 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
depa... (AT) bemis (DOT) com wrote:
This last statement throws an InvalidOperationException with "There is
an error in XML document (1, 92143)". The InnerException is "There is
an unclosed literal string. Line 1, position 92143". I can't really
think of a good way to get to position 92143 to see what the issue is.
I tried messing around with creating an XmlWriterSettings object and
changing the encoding to Unicode and turning of CheckCharacters, but
no dice.

Can you try to write out the XML with XmlWriterSettings and Indent set
to true? Then perhaps you get an error message with a line number and a
position which is easier to find than position 92143 in a single line.

Other than that I don't think you need to use the MemoryStream, you can
simply create an XmlNodeReader over your node e.g.
serializer = New XmlSerializer(myObjct.GetType())
myObject = serializer.Deserialize(New XmlNodeReader(node))
I don't know obviously if that way the problem vanishes but you could
try it and let us know.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
I tried changing the XmlWriter to indent, but was still unable to
determine what exactly was causing the issue - it was at a position
that seemed like valid XML to me. It was in the middle of the
document and the element above and below seemed okay as well.

But I did change the code to use a XmlNodeReader like you suggested
and it worked. So I'm not going to get too worked up about what could
potentially be wrong at this time. Thanks for the pointers.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.