如何在 WPF 应用程序中显示 XML 文件并让用户 edit/save 它?

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

所以我用 C# 编写了一个 WPF 程序,它从 .xml 文件中读取所有配置。

我想为用户提供一个基本界面,以便编辑 .xml 文件中的现有节点以及添加新节点。

我的 xml 文件示例:

<?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>

我尝试了不同的方法,例如将 DataGrid 绑定到 xml。

是否有一种简单的方法来加载一组特定的节点,例如 <Documents> 节点,允许用户编辑或添加它们,然后保存?

这是一个非常快速的演示应用程序..

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

它基本上会从名为 xml.xml 的文件(包含在演示代码中)中读取您的设置,并将数据显示在您可以对其进行编辑的 DataGrid 中。有一个保存按钮可以将您所做的更改保存回 xml.xml 文件....至于添加节点,演示代码还将添加新的 Documents\SystemSpecs 部分 ....