Home Forums WinForms controls Xceed Grid for WinForms save grid contents to xml Reply To: save grid contents to xml

User (Old forums)
Member
Post count: 23064

<code>XmlTextWriter writer = new XmlTextWriter(“grid.xml”, Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement(“grid”);
foreach(Xceed.Grid.DataRow row in grid.DataRows)
{
writer.WriteStartElement(“row”);
foreach(Xceed.Grid.Cell cell in row.Cells)
{
string strValue;
if (cell.Value == null || cell.Value is DBNull) strValue = “”;
else strValue = cell.Value.ToString();
writer.WriteAttributeString(cell.ParentColumn.FieldName, strValue);
}
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Close();</code>
This will produce an XML-file that looks like this:<code><?xml version=”1.0″?>
<grid>
<row field1=”value1″ field2=”value2″ field3=”value3″ />
<row field1=”value1″ field2=”value2″ field3=”value3″ />
<row field1=”value1″ field2=”value2″ field3=”value3″ />
</grid></code>

Imported from legacy forums. Posted by Tommy (had 219 views)