Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have a DataGrid and I want to export its data into XML, with custom Tags.

For example, a row having "full-page-heading" will have a <h1></h1> tag and so on. What I want is conditional tagging.

Please help.

What I have tried:

Nothing as of now as I am unable to proceed
Posted
Updated 9-May-18 20:44pm
Comments
Maciej Los 10-May-18 2:16am    
Does your DataGridView is bind with datasource? If yes, what kind of datasource: BindingSource, DataTable, DataSet?
Primo Chalice 10-May-18 2:29am    
It is DataSet

Maciej Los:

Does your DataGridView is bind with datasource? If yes, what kind of datasource: BindingSource, DataTable, DataSet?
Primo Chalice:

It is DataSet


In this case, the simplest way is to use: DataSet.WriteXml Method (System.Data)[^] and DataSet.ReadXml Method (System.Data)[^]
 
Share this answer
 
Comments
Wendelius 10-May-18 6:08am    
Exactly, a 5
Maciej Los 10-May-18 6:28am    
Thanks, Mika.
 
Share this answer
 
Comments
Primo Chalice 10-May-18 3:06am    
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}

object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}



I used this code and it worked, but I want to have custom tags for specific columns. How do I do that?
Wendelius 10-May-18 3:21am    
Not sure if I understand this correctly, but if you want to add some special info to the columns, you could use extended properties. Consider the following:
         System.Data.DataTable table = new System.Data.DataTable("TheTable");

         table.Columns.Add("Col1", typeof(string));
         table.Columns.Add("Col2", typeof(string));

         table.Columns["Col2"].ExtendedProperties.Add("MyProp", "Important column");


         table.Rows.Add("A", "B");
         table.Rows.Add("C", "D");

         table.WriteXml("C:\\test.xml", System.Data.XmlWriteMode.WriteSchema);
Primo Chalice 10-May-18 4:49am    
This is exactly what I needed. It works :). I am relieved. One more question. Here, I am entering the data in the rows manually. But I already have 13 columns and I have data there. So how do I read the pre-filled data and put it in table.Rows.Add()?
Wendelius 10-May-18 6:08am    
Do you mean you already have data in the data table? If so, have a look at DataTable.Merge Method (System.Data)[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900