You can also use XElement.Load(@"MyGridColor.xml"), see:
How to load XML from a file - LINQ to XML | Microsoft Docs[
^]
And:
Reading XML documents using LINQ[
^]
Example:
public static void WriteDataGridViewSettings(System.Windows.Forms.DataGridView dgv)
{
XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"\MyGridColor.xml", Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement(dgv.Name);
int LastRow = dgv.Rows.Count-1;
for (int i = 0; i < LastRow; i++)
{
writer.WriteStartElement("Row");
for (int j = 0; j < dgv.Columns.Count - 1; j++)
{
writer.WriteStartElement("CellColor");
writer.WriteString(dgv.Rows[i].Cells[j].Style.BackColor.Name);
writer.WriteEndElement();
writer.WriteStartElement("TextColor");
writer.WriteString(dgv.Rows[i].Cells[j].Style.ForeColor.Name);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
Then you can read the file simply like this:
XElement xml = XElement.Load("MyGridColor.xml");
Optional: to produce a more nicely formatted output to the xml file you can use:
writer.WriteWhitespace("\n");
See example here:
https://www.dotnetperls.com/xmltextwriter[
^]