Hi All !
I want to save or Export the content(Row And Column Cell) of a DatGridView To a XML file .how can i do it ? there is No DataSet Or DataTable for DataGridView .
thanks in advance for any Help !

Recommended Answers

There are a few threads that are about exporting to Xml using c#, take a look at those and try and apply them to suite your needs (its the same principle, put the data from the datagGrid into variables and export them in a structured tree format). if you run …

Jump to Post

Perhaps consider something more like this:

string GridToXml(DataGridView source, string root, string element)
{
    var doc = new XmlDocument();

    doc.LoadXml("<" + root + " />");

    foreach (var row in source.Rows)
    {
        var element = doc.CreateElement(element);

        foreach (var cell in row.Cells)
        {
            element.AppendChild(doc.CreateElement(cell.OwningColumn.HeaderText));
            element.LastChild.InnerText = cell.FormattedValue;
        }

        doc.Firstchild.AppendChild(element); …
Jump to Post

All 8 Replies

There are a few threads that are about exporting to Xml using c#, take a look at those and try and apply them to suite your needs (its the same principle, put the data from the datagGrid into variables and export them in a structured tree format). if you run into problems just post the code and issues back here.

Thanks for ur reply , Here My Code :

SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Xml files (*.xml)|*.xml";
            saveDialog.FilterIndex = 2;
            saveDialog.RestoreDirectory = true;
            saveDialog.InitialDirectory = "d:\\";
            saveDialog.FileName = "XML File";
            saveDialog.Title = "XML Export";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                string OutPutXMLFile = "";
                System.Xml.XmlDocument XMLDoc = new System.Xml.XmlDocument();
                for (int i = 0; i < DataGridView1.Rows.Count; i++)
                {
                    OutPutXMLFile = "";
                    for (int j = 0; j < DataGridView1.Columns.Count; j++)
                        OutPutXMLFile += String.Format("{0:0.00}", DataGridView1.Rows[i].Cells[j].Value);
                    XMLDoc.CreateElement(OutPutXMLFile);

                }
            }

But I Have some Error to create a xml file !
regards !

Perhaps consider something more like this:

string GridToXml(DataGridView source, string root, string element)
{
    var doc = new XmlDocument();

    doc.LoadXml("<" + root + " />");

    foreach (var row in source.Rows)
    {
        var element = doc.CreateElement(element);

        foreach (var cell in row.Cells)
        {
            element.AppendChild(doc.CreateElement(cell.OwningColumn.HeaderText));
            element.LastChild.InnerText = cell.FormattedValue;
        }

        doc.Firstchild.AppendChild(element);
    }

    return doc.OuterXml;
}

what's "var" in your code (in c#.net)?

I use C#.net 2005 and there is no keyword for "var" . apparently it added in 2010 (later) . is it equal "Type"?

Replace var with whatever type name you're assigning to the variable. var row in source.Rows would become DataGridViewRow row in source.Rows . You'd know that if you actually read some of the links from that search instead of just determining that var wasn't supported on your older compiler.

thanks for ur Help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.