Dataset.WriteXml and xml:space="preserve"
Why the method Dataset.WriteXml() produces xml with the attribute xml:space="preserve" in the elements containing empty strings?
Is there a way to avoid it?
Here is an example :
- Define a dataset named DatasetTest in dataset designer, with one datatable named Table1. Add two datacolumns named Column1 and Column2 to the datatable. The datatype of both colums is string.
- Call this code snippet :
Dim ds As New DatasetTest
ds.Table1.AddTable1Row("value1", "")
ds.WriteXml("C:\test.xml")
- The result is :
<?xml version="1.0" standalone="yes"?>
<DatasetTest xmlns="http://tempuri.org/DatasetTest.xsd">
<Table1>
<Column1>value1</Column1>
<Column2 xml:space="preserve" />
</Table1>
</DatasetTest>
This xml could non be validated by some applications, because of the attribute xml:space.
Daniele, Thanks for bringing this to our attention.
In v1.x, DataSet.ReadXml() could not preserve spaces in string DataColumns with SimpleContent ColumnMapping. In V2.0 in order to preserve spaces in such DataColumns DataSet was modified to emit XML with xml:space="preserve" attribute.
I see your point about supressing xml:preserve="true" for empty columns. We'll investigate this issue at our end and update this forum with the results.
We recognize writing of xml:space=preserve attribute for empty string columns as a bug. It will be fixed for .NET 2.0 RTM release; xml:preserve="true" will not be emitted for empty string columns.
Thanks,
Kawarjit Bedi
Thank you!
I will wait for 2.0 RTM release.
Another little question :
The dataset definition should have the declaration xmlns:xml="
http://www.w3.org/1998/xml or is it automatically declared?
if I open in VS2005 the xml file generated with WriteXml, the environment says to me that "The
'http://www.w3.org/XML/1998/namespace:space' attribute is not declared"
Is there a way to let VS2005 recognize the attribute for validating?
Thanks in Advance,
Daniele.
xmlns:xml="http://www.w3.org/1998/xml is automatically declared. The XML Editor will be fixed shortly so that it does not require this attribute be declared in the schema.
Was this fix in the november release because I still have <
xml:space="preserve"> on columns with empty string ?Thanks.
This appears to affect the way datasets work across web services in 2.0 as well. In 1.1, a field with just spaces in it was trimmed to an empty string. Now we're converting to 2.0 and they aren't empty strings anymore. Although 2.0 is correct, it breaks existing code in some locations.
Is there any way to easily modify how this works?
Sample code below that works differently in 1.1. and 2.0:
*** SERVICE ***
[WebMethod]
public DataSet GiveMeWhitespace()
{
DataSet testDS = new DataSet();
DataTable testTbl = new DataTable("MyTable");
testDS.Tables.Add(testTbl);
DataColumn testCol = new DataColumn("Col1", "abc".GetType());
testCol.AllowDBNull = true;
testTbl.Columns.Add(testCol);
DataRow newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = null;
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = " ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = " ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "A value";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "Another value";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "A value ";
newRow = testTbl.NewRow();
testTbl.Rows.Add(newRow);
newRow["Col1"] = "Another value ";
return testDS;
}
*** CLIENT ***
localhost.Service1 svc = new localhost.Service1();
DataSet testDS = svc.GiveMeWhitespace();
foreach (DataRow aRow in testDS.Tables[0].Rows)
{
if (!aRow.IsNull("Col1"))
{
TextBox1.Text = TextBox1.Text + "*" + aRow["Col1"].ToString() + "*\n";
}
else
{
TextBox1.Text = TextBox1.Text + "*{null}*\n";
}
}
I have the same problem with .NET 1.1. Worse, it changed my column name to "Table_Id" when I used ds.ReadXml. Is there any fix pack for .NET 1.1?
Thanks.
Xin
Looks like this issue is still around a year later. Will this "feature" ever be fixed? Is there a property somewhere I can set to turn this off?
HELP!
I now have the same issue - customers saving datasets to XML in .NET CF 2 find the files they create can't be read by other devices and PCs. So much for universal data - how can I get this fixed? Sounds like I'm not alone but that doesn't help my customers. I need the xml

pace="preserve" to be removed or otherwise need some fix that will allow these files to be read by earlier versions of the XMLreader. Please help . . . .
I still face this issue in .net 2.0 and when such datasets are written to an Excel file thru WriteXML, it makes an extra column with ns1

pace as the column heading and the value as preserve. Has there been any fix to this issue from micorsoft yet?
This is the exact same problem that I am facing now. We just upgraded our webservices to .net 2.0 and this was not caught in testing.
Please can I be notified when a fix for this is available.
Does anyone know of any workarounds?
Thanks!