DataSet.WriteXML writes strange node names, why?

  • Thread starter Thread starter Sin Jeong-hun
  • Start date Start date
S

Sin Jeong-hun

This code,
DS1 = new DataSet("DataSet 1");
DataTable dt1 = new DataTable("Table 1");
DataColumn dc1 = new DataColumn("Column 1",
typeof(String));
DataColumn dc2 = new DataColumn("Column 2",
typeof(String));
dt1.Columns.AddRange(new DataColumn[] { dc1, dc2 });
dt1.Rows.Add(new string[] { "A", "B" });
dt1.Rows.Add(new string[] { "C", "D" });
DS1.Tables.Add(dt1);
DS1.WriteXml("test.xml");
creates an xml like,
<?xml version="1.0" standalone="yes"?>
<DataSet_x0020_1>
<Table_x0020_1>
<Column_x0020_1>A</Column_x0020_1>
<Column_x0020_2>B</Column_x0020_2>
</Table_x0020_1>
<Table_x0020_1>
<Column_x0020_1>C</Column_x0020_1>
<Column_x0020_2>D</Column_x0020_2>
</Table_x0020_1>
</DataSet_x0020_1>
Why is this? Of course I expected "Column 1", instead of
"Column_x0020_1". I could not figure out the reason, please give me a
hint. Thanks.
 
You have space characters imbedded in the names you have provided for your
Dataset, DataTable and DataColumns.

XML does not allow a space character in an element name and therefore, the
code behind the WriteXml method has written a hex representation of your
space characters so that the element names are legal.
 
You have space characters imbedded in the names you have provided for your
Dataset, DataTable and DataColumns.

XML does not allow a space character in an element name and therefore, the
code behind the WriteXml method has written a hex representation of your
space characters so that the element names are legal.




This code,
           DS1 = new DataSet("DataSet 1");
           DataTable dt1 = new DataTable("Table 1");
           DataColumn dc1 = new DataColumn("Column 1",
typeof(String));
           DataColumn dc2 = new DataColumn("Column 2",
typeof(String));
           dt1.Columns.AddRange(new DataColumn[] { dc1, dc2 });
           dt1.Rows.Add(new string[] { "A", "B" });
           dt1.Rows.Add(new string[] { "C", "D" });
           DS1.Tables.Add(dt1);
           DS1.WriteXml("test.xml");
creates an xml like,
<?xml version="1.0" standalone="yes"?>
<DataSet_x0020_1>
  <Table_x0020_1>
<Column_x0020_1>A</Column_x0020_1>
<Column_x0020_2>B</Column_x0020_2>
  </Table_x0020_1>
  <Table_x0020_1>
<Column_x0020_1>C</Column_x0020_1>
<Column_x0020_2>D</Column_x0020_2>
  </Table_x0020_1>
</DataSet_x0020_1>
Why is this? Of course I expected "Column 1", instead of
"Column_x0020_1". I could not figure out the reason, please give me a
hint. Thanks.

Ah, thanks. I forgot XML node name cannot contain spaces.
 
Back
Top