Search code examples
c#xmlwpfwpfdatagrid

How do you display an XML file in a WPF App and let the user edit/save it?


So I wrote a WPF program in C# and it reads all of it's configurations from an .xml file.

I would like to provide the user with a basic interface in order to edit existing nodes in the .xml file as well as add new ones.

An example of my xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Configurations>
    <Documents>
        <MenuChoice>Drawing</MenuChoice>
        <Body>A drawing</Body>
        <Cost>0</Cost>
    </Documents>
    <Documents>
        <MenuChoice>Diagram</MenuChoice>
        <Body>A diagram</Body>
        <Cost>0</Cost>
    </Documents>
    <SystemSpecs>
        <MenuChoice>spec1</MenuChoice>
        <Body>spec1 body</Body>
        <Cost>0</Cost>
    </SystemSpecs>
    <SystemSpecs>
        <MenuChoice>spec2</MenuChoice>
        <Body>spec2 body</Body>
        <Cost>0</Cost>
    </SystemSpecs>
</Configurations>

I've tried different things such as binding a DataGrid to the xml for instance.

Is there an easy way to load up a specific set of nodes like the <Documents> nodes, allow the user to edit them or add to them, then save it?


Solution

  • Here is a very quick demo application..

    http://www.mediafire.com/download/wv67fv6y2vpe1do/ConfigEditor_Demo.rar

    it will essentially read your settings from a file called xml.xml (included with the demo code) and display the data in a DataGrid where you can edit it. There is a save button that will save the changes you have made back to the xml.xml file.... as for adding nodes, The demo code will add new Documents\SystemSpecs sections also ....