How to insert multiple datatable value to an xml file?

Posted by Tripati_tutu under XML on 3/28/2011 | Points: 10 | Views : 4426 | Status : [Member] | Replies : 3
Here I am getting multiple records from different sections by using different datatables.
Like I have dTableCustomer, dTableAdministrator etc. So I am getting different datatables.
So how to show that records into the xml file, means I will write the code in a manner so that all the records will show in a single xml file.

its urgent. If anyone have idea send me immediately then I will thankful to you.




Responses

Posted by: T.saravanan on: 3/28/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

If Column Name is common in both datatable means just using 'Merge' method for your scenario...
Ex:
DataTable dt1 = new DataTable("Table1"); 

DataTable dt2 = new DataTable("Table2");
dt1.Merge(dt2);
dt1.WriteXml(@"C:\test.xml");


else using looping concept to union both datatable values into single datatable then write into XML.



Thanks,
T.Saravanan

Tripati_tutu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tripati_tutu on: 3/28/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Saravanan,

Thanks for your response. But what I mean to say is lets consider I m getting DataTable1, DataTable2, DataTable3, ..... records.
Eg. In my code file, I have written below like code
string path = "~/App_Data/Customer.xml";
path = Server.MapPath(path);
WriteXMLFile(DataTable1, path);
WriteXMLFile(DataTable2, path);
WriteXMLFile(DataTable3, path);

for this I have declared one method,
private void WriteXMLFile(DataTable dTable, string path)
{
System.Text.StringBuilder strB = new System.Text.StringBuilder("<?xml version=\"1.0\" standalone=\"yes\"?>", 1000);
strB.Append("<Customer>");
foreach (DataRow row in dTable.Rows)
{
strB.Append("<Category AutoID=\"" + row["AutoID"] + "\" Name = \"" + row["Title"] + "\" />");
}
strB.Append("</Customer>");
System.IO.File.WriteAllText(path, strB.ToString());
}

Here I want to show all three datatable records into the customer.xml
If any idea then let me know...

Tripati_tutu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 3/28/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Analyze your code,So all the datatable column names are same then why should not use 'Merge' function.
DataTable1.Merge(DataTable2);

DataTable1.Merge(DataTable3);

Now you pass DataTable1 into that method.

else just add all the datatable values into one dataset then pass that value like....
Dataset dsData = new Dataset();

dsData.Tables.Add(DataTable1);
dsData.Tables.Add(DataTable2);
dsData.Tables.Add(DataTable3);

private void WriteXMLFile(Dataset dTable, string path)
{
System.Text.StringBuilder strB = new System.Text.StringBuilder("<?xml version=\"1.0\" standalone=\"yes\"?>", 1000);
strB.Append("<Customer>");
for(int x=0;x<dTable.Tables.Count;x++)
{
foreach (DataRow row in dTable.Tables[x].Rows)
{
strB.Append("<Category AutoID=\"" + row["AutoID"] + "\" Name = \"" + row["Title"] + "\" />");
}
}

strB.Append("</Customer>");
System.IO.File.WriteAllText(path, strB.ToString());
}


Try this....


Thanks,
T.Saravanan

Tripati_tutu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response