cvenko
cvenko

Reputation: 141

WinForm look to XML file

I need to write the look of a simple WinForm to XML code using C#. For example, I have a form with two buttons, listview, treeview, groupbox, menu and a panel.

Later on, I need to read that file and it has to reconstruct exactly the same WinForm.

Any ideas on how to approach this matter? I've seen a similar post about matter, but it only describes how to write values, not positioning, size, etc...

This is anexample, how the data written in xml file should look like

Upvotes: 2

Views: 300

Answers (1)

L.B
L.B

Reputation: 116168

I would use LinqToXml here.....

XElement root = new XElement("Form");
TraverseAllControls(root, this);
var xml = root.ToString();

void TraverseAllControls(XElement xElem,Control ctrl)
{
    foreach (Control c in ctrl.Controls)
    {
        if (String.IsNullOrEmpty(c.Name)) continue;

        var e = new XElement(c.Name, 
                    new XElement("Width",c.Width), 
                    new XElement("Height",c.Height),
                    new XElement("X",c.Location.X),
                    new XElement("Y",c.Location.Y));
        xElem.Add(e);
        TraverseAllControls(e, c);
    }
}

Upvotes: 4

Related Questions