write data of unbound datagridview to XML

A

Anthoni

Dear All,
I need to export data of unbound datagridview to XML file.
Since my DataSource is NULL, What other options do I have?
Does i need to make 2 loops (on all the records and inside loop of all
the cells)
read data and export it to XML?
TIA,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I see no other option.

You could define a Dataset/DataTable with the correct definition and then
just iterate in the Datagridview rows.

At the end you just use DataSet.WriteXml
 
M

Marc Gravell

Any reason it must be unbound? If you could declare a simple class to
hold the values, and have a List<YourClass> or BindingList<YourClass>
as the data, then you should be able to simple use the XmlSerializer /
DataContractSerializer to get fairly standard xml.

Marc
 
A

Anthoni

Thanks,
It seems that I need to do all the work, the problem is that my
knowlagde in XML is very poor.
therefore, in order to get somthing like
<?xml version="1.0" encoding="iso-8859-1" ?>
<items>
<item>
<item_name> SOME NAME </item_name>
<item_description> DESCRIPTION</item_description>
</item>
<item>
<item_name> SOME OTHER_NAME </item_name>
<item_description> OTHER_DESCRIPTION</item_description>
</item>
</items>
Any simple sample to do it?
TIA,
 
M

Marc Gravell

As a very simple (and equally: untested) example (using XmlWriter):

using (XmlWriter writer = XmlWriter.Create("blahfile.xml"))
{
writer.WriteStartElement("items");
foreach(blah in blahs) {
writer.WriteStartElement("item");
// perhaps a foreach on columns here...
writer.WriteElementString("item_name", blahName);
writer.WriteElementString("item_description",
blahDescription);
writer.WriteEndElement(); // end of "item"
}
writer.WriteEndElement(); // end of "items"
writer.Close();
}

XmlWriter can write to files, streams, text-writers and
string-builders etc - it isn't limited to files. Obviously you need to
change every "blah" to something more useful. If you choose to loop on
the columns, you might be able to use part of the column headers for
the names?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top