formatting XML problem
-
DataSet datas = new DataSet("DataSetName"); DataTable dataTable = new DataTable("myNewTableName"); DataRow dataRow = dataTable.NewRow(); dataTable.Rows.Add(dataRow); for(int i = 0; i<5; i++) { DataColumn dataColumn = new DataColumn();; dataColumn.ColumnName = "myColumnName"+i.ToString(); dataTable.Columns.Add(dataColumn); dataRow["myColumnName"+i.ToString()]="myItem"+i.ToString(); } dataTable.AcceptChanges(); datas.Tables.Add(dataTable); dataGrid1.DataSource=dataTable; datas.WriteXml("TestXml.xml");
This code generate the following xml file: -------------- myItem0 myItem1 myItem2 myItem3 myItem4 -------------- I need the same data to be formatted in this way: -------------- -------------- how can I do this? thanks _____________ http://members.xoom.virgilio.it/yuppygames/english/index.htm -
DataSet datas = new DataSet("DataSetName"); DataTable dataTable = new DataTable("myNewTableName"); DataRow dataRow = dataTable.NewRow(); dataTable.Rows.Add(dataRow); for(int i = 0; i<5; i++) { DataColumn dataColumn = new DataColumn();; dataColumn.ColumnName = "myColumnName"+i.ToString(); dataTable.Columns.Add(dataColumn); dataRow["myColumnName"+i.ToString()]="myItem"+i.ToString(); } dataTable.AcceptChanges(); datas.Tables.Add(dataTable); dataGrid1.DataSource=dataTable; datas.WriteXml("TestXml.xml");
This code generate the following xml file: -------------- myItem0 myItem1 myItem2 myItem3 myItem4 -------------- I need the same data to be formatted in this way: -------------- -------------- how can I do this? thanks _____________ http://members.xoom.virgilio.it/yuppygames/english/index.htmDon't use a
DataSet
. It outputs XML in specific ways - namely as elements. Instead, see the classes in theSystem.Xml
namespace. You could enumerate the tables and columns in yourDataSet
(or use anXmlDataDocument
) and then write them out to a file using anXmlTextWriter
, which allows you to write attributes, elements, comments, and even processing instructions however you want. Lots of samples are included in the class documentation for theSystem.Xml
namespace. One other option is to save this to a file or aMemoryStream
and then load it into anXmlDocument
or some other class. You could then use aXslTransform
to transform the elements into attributes.Microsoft MVP, Visual C# My Articles