C# .NET Write XML File from SQL Dataset with Custom Schema -
i have following code in c# visual studio 2010.
string connectionstring = "provider=microsoft.jet.oledb.4.0;data source=c:\\database\\db.mde"; string sql = "select * customer"; oledbconnection connection = new oledbconnection(connectionstring); oledbdataadapter adapter = new oledbdataadapter(sql, connection); dataset ds = new dataset(); connection.open(); adapter.fill(ds, "test table"); connection.close(); datagridview1.datasource = ds; datagridview1.datamember = "test table"; ds.writexml("c:\\users\\desktop\\testfile.xml");
now want need modify format of xml file when exports. can done? gather need provide schema file fine i'm not sure how implement dataset.
currently xml looks this.
<?xml version="1.0" standalone="yes"?> <newdataset> <test_x0020_table> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </test_x0020_table> </newdataset>
... want this
<?xml version="1.0" standalone="yes"?> <customers> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </customers>
any appreciated.
the first node name of table.
you can change setting name on dataset.
dataset ds = new dataset("customers"); //plural
the inner node record.
you can change specifying name when fill adapter.
adapter.fill(ds, "customer"); //singular
this give result like:
<customers> <customer> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </customer> </customers>
this means if return multiple results end with:
<customers> <customer> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </customer> <customer> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </customer> <customer> <name>customer1</name> <address>25 big st</address> <suburb>sydney nsw</suburb> <contact>fred nurk</contact> <phone>11 1111 1111</phone> </customer> </customers>
you can't have single "customer" because when write xml it's formatting "table" "records".
Comments
Post a Comment