By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

XML deserialize Stream works ok but deserialize from XmlNodeReader fails

P: n/a

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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.)

Nov 21 '05 #2

P: n/a
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.)

Nov 21 '05 #3

P: n/a
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
On Mon, 07 Feb 2005 01:29:40 GMT, v-******@online.microsoft.com
(Steven Cheng[MSFT]) wrote:
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


Nov 21 '05 #4

P: n/a
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.)

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.