views:

66

answers:

2

I have the following code that serializes a DataTable to XML.

StringWriter sw = new StringWriter();
myDataTable.WriteXml(sw);

And this works, however, the serialized XML looks like this:

<NameOfTable>
    <NameOfTable>
        <ID>1</ID>
        <Name>Jack</Name>
    </NameOfTable>
    <NameOfTable>
        <ID>2</ID>
        <Name>Frank</Name>
    </NameOfTable>
</NameOfTable>

Is there anyway to change the outer <NameOfTable> to, say, <Records> and the inner <NameOfTable> to <Person>?

+2  A: 

This does what you asked. But I don't think it does what you want.

    DataSet ds = new DataSet("Records");
    DataTable dt = new DataTable("Person");
    ds.Tables.Add(dt);
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");

    dt.Rows.Add(new object[] { 1, "Jack" });
    dt.Rows.Add(new object[] { 2, "Frank" });

    StringWriter sw = new StringWriter();
    dt.WriteXml(sw);

    MessageBox.Show(sw.ToString());
gbogumil
You are wrong. Your code does exactly what I want. Thanks.
AngryHacker
A: 

Before you write the table to XML you could change the column names in the data table. Not optimal but it would do what you want.

Justin