Deserializing DataSet With White Space - How To?

T

Tom Jastrzebski

Hello everybody,



It looks like this is a known problem, but I found no solution.
Deserialization of DataSet object from XML does not preserve white space.

The same code executed under .Net Framework 2.0 works, so apparently problem
got fixed. As a fix xml:space="preserve" attribute is being added where
appropriate.



Here are two questions:



1. Consequently, DataSet serialized under .Net Framework 2.0 does NOT
deserialize under 1.1. Is/will it be possible to maintain backwards
compatibility, so data can be exchanged between different versions of MS
..Net Framework?



2. More important to me right now: is there any way to deserialize Data set
from XML and preserve white space under .Net Famework 1.1?



Thanks,

Tom





Sample code demonstrating the problem - it passes under .Net 2.0 but fails
under 1.1

- btw, using XmlTextReader does not help



using System;
using System.Data;
using System.IO;

class Program
{
static void Main(string[] args)
{
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
ds1.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Rows.Add(new object[] { " " }); // add row containing single
space

// serialize
MemoryStream ms = new MemoryStream();
ds1.WriteXml(ms);



// deserialize
ms.Position = 0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms);

string value = (string)ds2.Tables[0].Rows[0]["column1"];

if (value == " ") { // test if row contains single space
Console.WriteLine("passed");
} else {
Console.WriteLine("failed");
}
Console.Read();

}
}
 
S

Sahil Malik [MVP]

Tom,

Look up "DatasetSurrogate" - that does not have this issue, and it will work
with .NET 1.1 (plus it has a few other advantages like much better
performance).

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
 
T

Tom Jastrzebski

Sahil,

Thank you for your answer.
However, I am not sure it helps solves my problem. It looks like
DatasetSurrogate is designed to serialize data to/from binary format, while
I am only interested in XML. Am I missing something?

Tom


Sahil Malik said:
Tom,

Look up "DatasetSurrogate" - that does not have this issue, and it will
work with .NET 1.1 (plus it has a few other advantages like much better
performance).

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Tom Jastrzebski said:
Hello everybody,



It looks like this is a known problem, but I found no solution.
Deserialization of DataSet object from XML does not preserve white space.

The same code executed under .Net Framework 2.0 works, so apparently
problem got fixed. As a fix xml:space="preserve" attribute is being
added where appropriate.



Here are two questions:



1. Consequently, DataSet serialized under .Net Framework 2.0 does NOT
deserialize under 1.1. Is/will it be possible to maintain backwards
compatibility, so data can be exchanged between different versions of MS
.Net Framework?



2. More important to me right now: is there any way to deserialize Data
set from XML and preserve white space under .Net Famework 1.1?



Thanks,

Tom





Sample code demonstrating the problem - it passes under .Net 2.0 but
fails under 1.1

- btw, using XmlTextReader does not help



using System;
using System.Data;
using System.IO;

class Program
{
static void Main(string[] args)
{
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
ds1.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Rows.Add(new object[] { " " }); // add row containing single
space

// serialize
MemoryStream ms = new MemoryStream();
ds1.WriteXml(ms);



// deserialize
ms.Position = 0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms);

string value = (string)ds2.Tables[0].Rows[0]["column1"];

if (value == " ") { // test if row contains single space
Console.WriteLine("passed");
} else {
Console.WriteLine("failed");
}
Console.Read();

}
}
 
S

Sahil Malik [MVP]

Actually you aren't missing anything. I figured that due to the
serialization/deserialization process your data is getting mangled - which
the datasetsurrogate will prevent. But I think you want the final output as
XML - right?

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Tom Jastrzebski said:
Sahil,

Thank you for your answer.
However, I am not sure it helps solves my problem. It looks like
DatasetSurrogate is designed to serialize data to/from binary format,
while I am only interested in XML. Am I missing something?

Tom


Sahil Malik said:
Tom,

Look up "DatasetSurrogate" - that does not have this issue, and it will
work with .NET 1.1 (plus it has a few other advantages like much better
performance).

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Tom Jastrzebski said:
Hello everybody,



It looks like this is a known problem, but I found no solution.
Deserialization of DataSet object from XML does not preserve white
space.

The same code executed under .Net Framework 2.0 works, so apparently
problem got fixed. As a fix xml:space="preserve" attribute is being
added where appropriate.



Here are two questions:



1. Consequently, DataSet serialized under .Net Framework 2.0 does NOT
deserialize under 1.1. Is/will it be possible to maintain backwards
compatibility, so data can be exchanged between different versions of MS
.Net Framework?



2. More important to me right now: is there any way to deserialize Data
set from XML and preserve white space under .Net Famework 1.1?



Thanks,

Tom





Sample code demonstrating the problem - it passes under .Net 2.0 but
fails under 1.1

- btw, using XmlTextReader does not help



using System;
using System.Data;
using System.IO;

class Program
{
static void Main(string[] args)
{
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
ds1.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Rows.Add(new object[] { " " }); // add row containing single
space

// serialize
MemoryStream ms = new MemoryStream();
ds1.WriteXml(ms);



// deserialize
ms.Position = 0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms);

string value = (string)ds2.Tables[0].Rows[0]["column1"];

if (value == " ") { // test if row contains single space
Console.WriteLine("passed");
} else {
Console.WriteLine("failed");
}
Console.Read();

}
}
 
T

Tom Jastrzebski

Right, my data hast to be serialized to XML - that's a requirement.
What causes the problem is actually deserialization, rather than
serialization.
I guess it is one of those bugs which will never be fixed due to low
exposure.

Tom


Sahil Malik said:
Actually you aren't missing anything. I figured that due to the
serialization/deserialization process your data is getting mangled - which
the datasetsurrogate will prevent. But I think you want the final output
as XML - right?

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Tom Jastrzebski said:
Sahil,

Thank you for your answer.
However, I am not sure it helps solves my problem. It looks like
DatasetSurrogate is designed to serialize data to/from binary format,
while I am only interested in XML. Am I missing something?

Tom


Sahil Malik said:
Tom,

Look up "DatasetSurrogate" - that does not have this issue, and it will
work with .NET 1.1 (plus it has a few other advantages like much better
performance).

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Hello everybody,



It looks like this is a known problem, but I found no solution.
Deserialization of DataSet object from XML does not preserve white
space.

The same code executed under .Net Framework 2.0 works, so apparently
problem got fixed. As a fix xml:space="preserve" attribute is being
added where appropriate.



Here are two questions:



1. Consequently, DataSet serialized under .Net Framework 2.0 does NOT
deserialize under 1.1. Is/will it be possible to maintain backwards
compatibility, so data can be exchanged between different versions of
MS .Net Framework?



2. More important to me right now: is there any way to deserialize Data
set from XML and preserve white space under .Net Famework 1.1?



Thanks,

Tom





Sample code demonstrating the problem - it passes under .Net 2.0 but
fails under 1.1

- btw, using XmlTextReader does not help



using System;
using System.Data;
using System.IO;

class Program
{
static void Main(string[] args)
{
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
ds1.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Rows.Add(new object[] { " " }); // add row containing single
space

// serialize
MemoryStream ms = new MemoryStream();
ds1.WriteXml(ms);



// deserialize
ms.Position = 0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms);

string value = (string)ds2.Tables[0].Rows[0]["column1"];

if (value == " ") { // test if row contains single space
Console.WriteLine("passed");
} else {
Console.WriteLine("failed");
}
Console.Read();

}
}
 
S

Sahil Malik [MVP]

I think this is a hard problem to solve. I believe SQL Server,
XmlSerialization etc - all just ignore whitespace, but in those there are
methods to tell not to discard whitespace, I am not quite sure what you'd
have to do with a Dataset.

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------
---------------

Tom Jastrzebski said:
Right, my data hast to be serialized to XML - that's a requirement.
What causes the problem is actually deserialization, rather than
serialization.
I guess it is one of those bugs which will never be fixed due to low
exposure.

Tom


Sahil Malik said:
Actually you aren't missing anything. I figured that due to the
serialization/deserialization process your data is getting mangled - which
the datasetsurrogate will prevent. But I think you want the final output
as XML - right?

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
--------------------------------------------------------------------------
--
Tom Jastrzebski said:
Sahil,

Thank you for your answer.
However, I am not sure it helps solves my problem. It looks like
DatasetSurrogate is designed to serialize data to/from binary format,
while I am only interested in XML. Am I missing something?

Tom


Tom,

Look up "DatasetSurrogate" - that does not have this issue, and it will
work with .NET 1.1 (plus it has a few other advantages like much better
performance).

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
------------------------------------------------------------------------ ----

Hello everybody,



It looks like this is a known problem, but I found no solution.
Deserialization of DataSet object from XML does not preserve white
space.

The same code executed under .Net Framework 2.0 works, so apparently
problem got fixed. As a fix xml:space="preserve" attribute is being
added where appropriate.



Here are two questions:



1. Consequently, DataSet serialized under .Net Framework 2.0 does NOT
deserialize under 1.1. Is/will it be possible to maintain backwards
compatibility, so data can be exchanged between different versions of
MS .Net Framework?



2. More important to me right now: is there any way to deserialize Data
set from XML and preserve white space under .Net Famework 1.1?



Thanks,

Tom





Sample code demonstrating the problem - it passes under .Net 2.0 but
fails under 1.1

- btw, using XmlTextReader does not help



using System;
using System.Data;
using System.IO;

class Program
{
static void Main(string[] args)
{
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
ds1.Tables.Add(dt);
dt.Columns.Add("column1", typeof(string));
dt.Rows.Add(new object[] { " " }); // add row containing single
space

// serialize
MemoryStream ms = new MemoryStream();
ds1.WriteXml(ms);



// deserialize
ms.Position = 0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms);

string value = (string)ds2.Tables[0].Rows[0]["column1"];

if (value == " ") { // test if row contains single space
Console.WriteLine("passed");
} else {
Console.WriteLine("failed");
}
Console.Read();

}
}
 

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