XML deserialize Stream works ok but deserialize from XmlNodeReader fails

S

Samuel R. Neff

I'm deserializing an XML file. If I pass a Stream to the file
directly to the deserializer as follows it works fine:


o = (New XmlSerializer( GetType(
CompensationPackage))).Deserialize(stream)


But if I pass an XmlNodeReader to the deserializer it doesn't work:


Dim doc As New XmlDocument
doc.Load(stream)
Dim reader As New XmlNodeReader(doc)
o = (New XmlSerializer( GetType(
CompensationPackage))).Deserialize(reader)


The problem that occurs is that the XML file has a DataSet inside it
(within a large object hierarchy--it's not just a dataset) and when I
deserialize via an XmlNodeReader the DataSet comes through without any
tables. It exists, but the data isn't there.

The reason I want to use an XmlNodeReader instead of just using the
Stream directly is I need to read one of the root attributes first to
verify the version before attempting to deserialize (run a transform
if it's an old version, reject if it's too new).

Here's a heavily snipped version of the xml file...

<?xml version="1.0" encoding="utf-8"?>
<CompensationPackage xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<InternalTables>
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Salary">
<xs:complexType>
<xs:sequence>
<xs:element name="_x0030_" type="xs:double"
minOccurs="0" />
<xs:element name="_x0031_" type="xs:double"
minOccurs="0" />
<xs:element name="_x0032_" type="xs:double"
minOccurs="0" />
<xs:element name="_x0033_" type="xs:double"
minOccurs="0" />
<xs:element name="_x0034_" type="xs:double"
minOccurs="0" />
<xs:element name="_x0035_" type="xs:double"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<Salary diffgr:id="Salary1" msdata:rowOrder="0">
<_x0030_>22000</_x0030_>
<_x0031_>23320</_x0031_>
<_x0032_>24640</_x0032_>
<_x0033_>25960</_x0033_>
<_x0034_>27280</_x0034_>
<_x0035_>29040</_x0035_>
</Salary>
<Salary diffgr:id="Salary2" msdata:rowOrder="1">
<_x0030_>22660</_x0030_>
<_x0031_>24019.6</_x0031_>
<_x0032_>25379.2</_x0032_>
<_x0033_>26738.8</_x0033_>
<_x0034_>28098.4</_x0034_>
<_x0035_>30346.8</_x0035_>
</Salary>
</NewDataSet>
</diffgr:diffgram>
</InternalTables>
</SalarySchedule>
</CompensationPackage>

Thanks,

Sam
 
S

Steven Cheng[MSFT]

Hi Sam,

Thanks for your posting. Regarding on the problem you mentioned, I've also
done some tests on myside and did found the problem you mentioned. It seems
that when deserializing such a typed dataset through the XmlNodeReader,
we'll lose the Table elements. Currently I'm doing some further research to
see whether this is a known issue or can be resolved, I'll update you as
soon as I got any new info.
Thanks for your understanding.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Sam,

Sorry for keeping you waiting. After some further research, the problem
when deserializing DataSet through a XmlNodeReader does due to an exsiting
issue. Your expected result that all the XmlReader's concrete derived class
should work is reasonable. And our dev guys plans to fix the problem in the
whidbey version of the .net framework. Currently we may need to use other
XmlReader to workaround this. I'm sorry for any inconvenience it brings
you.

Thanks & Regards,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Samuel R. Neff

Steven,

Thanks for the aditional information but it doesn't really help with
the problem now. I can't wait for the next version of the framework
for my program.

Can you tell me exactly where the problem lies, how it was fixed in
widbey, and point me towards a work-around in the current framework?
Is there another XmlReader that can read a document already in memory
or do I need to write the document out as a sting or text stream and
re-parse it, which would very waisteful.

Thanks,

Sam
 
S

Steven Cheng[MSFT]

Hi Samuel,

Thanks for your followup. The problem with currently XmlSerializer seems
caused by something incorrect when handling the namespace prefix (using the
xmlNodeReader).

Currently, one workaround is to use a StringReader to wrapper the
Node.OuterXml and deserialize on the StringREader rather than the
XmlNodeReader( for DataSet Type).
For example:

XmlDocument doc = new XmlDocument();
doc.Load("output.xml");

StringReader sr = new StringReader(doc.OuterXml);
MyDataSet ds = serializer.Deserialize(sr) as MyDataSet;

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top