Answered by:
Dataset WriteXML issue - change Node/Element Name

Question
-
Hi all,
I am wondering if anywayI can change the node/element name when using WriteXml method inC# code? Say, I have a table in a dataset
Column1 Coulmn2 Coulmn3
123 234 345
when I use WriteXml method to generate a xml file, it alwasy looks like:
<NewDataset>
<Table>
<Column1>123</Column1>
<Column2>234</Column2>
<Column3>345</Column3>
</Table>
</NewDataset>
While, I want it to be something like this:
<NewDataset>
<Table>
<Data>123</Data>
<Data>234</Data>
<Data>345</Data>
</Table>
</NewDataset>
Is that possible? how to make it happen?
Thanks in advance!
4WD- Edited by Happy4WD Monday, May 11, 2009 6:01 PM
Monday, May 11, 2009 5:57 PM
Answers
-
Not using the DataTable.WriteXml.
You can write out your own file.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Proposed as answer by Marcelo Lopez Ruiz - MSFTMicrosoft employee, Editor Tuesday, May 12, 2009 5:08 AM
- Marked as answer by Arun Chandrasekhar - MSFTMicrosoft employee, Moderator Thursday, May 12, 2011 8:04 AM
Tuesday, May 12, 2009 2:29 AM
All replies
-
Change the column name of the DataTable before you write it out.
You cannot have the same name though else you will get a DuplicateNameException. You can have Data1, Data2, etc....
//Example
DataTable table = new DataTable("Table");
table.Columns.Add(new DataColumn("Column1", typeof(Int32)));
table.Columns.Add(new DataColumn("Column2", typeof(Int32)));
table.Columns.Add(new DataColumn("Column3", typeof(Int32)));
table.Rows.Add(123, 234, 345);
DataSet ds = new DataSet("NewDataSet");
ds.Tables.Add(table);
ds.WriteXml("1.xml");//Now let's change it
ds.Tables["Table"].Columns[0].ColumnName = "Data1";
ds.Tables["Table"].Columns[1].ColumnName = "Data2";
ds.Tables["Table"].Columns[2].ColumnName = "Data3";
ds.WriteXml("2.xml");
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
- Edited by JohnGrove Monday, May 11, 2009 10:52 PM
Monday, May 11, 2009 10:42 PM -
There is no way to the same name like
<NewDataset>
<Table>
<Data>123</Data>
<Data>234</Data>
<Data>345</Data>
</Table>
</NewDataset>
Thanks!Tuesday, May 12, 2009 2:25 AM -
Not using the DataTable.WriteXml.
You can write out your own file.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Proposed as answer by Marcelo Lopez Ruiz - MSFTMicrosoft employee, Editor Tuesday, May 12, 2009 5:08 AM
- Marked as answer by Arun Chandrasekhar - MSFTMicrosoft employee, Moderator Thursday, May 12, 2011 8:04 AM
Tuesday, May 12, 2009 2:29 AM -
got ya, thanks John!!Tuesday, May 12, 2009 3:14 AM