简体   繁体   English

C#xmldataprovider插入节点

[英]C# xmldataprovider insert node

I am trying to write an editor for an xml config file. 我正在尝试为xml配置文件编写编辑器。 I have the xml file bound to a listview and selecting an element and then clicking edit allows you to edit an element. 我将xml文件绑定到列表视图并选择一个元素,然后单击“编辑”使您可以编辑元素。 When the user clicks save, a delegate is called which updates the element within the xml document. 当用户单击“保存”时,将调用一个委托,该委托将更新xml文档中的元素。 I have tried to do the updating in various ways including appendnode, prependnode, insertafter, and replace child. 我试图以各种方式进行更新,包括appendnode,prependnode,insertafter和replace child。 I have gotten them all working without any compile time or runtime errors, yet they don't update the xmldataprovider or the listview. 我已经使它们全部正常工作,没有任何编译时或运行时错误,但是它们不会更新xmldataprovider或listview。 It's probably just something simple that I am missing but I am having trouble figuring out what it is. 我可能想念的只是简单的事情,但是我很难弄清楚它是什么。 Can someone please help? 有人可以帮忙吗?

Thanks, Brian 谢谢,布莱恩

If it helps, here's the source code of my main form (the one with the listview) 如果有帮助,这是我的主要表单的源代码(带有listview的源代码)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;

namespace g2config
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        public XmlDataProvider dp;
        string cfgfile;
        public managecmpnts manage_components;
        public delegate void managecmpnts(int id, XmlElement component);


        public MainWindow()
        {
            InitializeComponent();
            dp = this.FindResource("configfile") as XmlDataProvider;

            cfgfile = "C:\\Users\\briansvgs\\Desktop\\g2config\\g2config\\bin\\Debug\\g2config.pxml";

            if(Environment.GetCommandLineArgs().Count() > 1)
            {
                string path = Environment.GetCommandLineArgs().ElementAt(1);
                //MessageBox.Show("Path: " + path);
                cfgfile = path;
            }

            if (File.Exists(cfgfile))
            {
                dp.Source = new Uri(cfgfile, UriKind.RelativeOrAbsolute);
            }
            else
            {
                MessageBox.Show("config file not found");
            }

            this.Title = this.Title + " (" + cfgfile + ") ";
        }

        public void browsedir( object sender, EventArgs e)
        {
              System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "PXML Files (*.pxml)|*.pxml"; ;

            //http://www.kirupa.com/net/using_open_file_dialog_pg4.htm
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //how to browse dirs instead of files?????
            {
                 startpath.Text = ofd.FileName;
            }
        }

        public void newcomponent(object sender, RoutedEventArgs e)
        {            
            componentsettings new_cmpnt = new componentsettings();
            new_cmpnt.Title = "Add Component";
            new_cmpnt.ShowDialog();         
        }

        public void editcomponent(object sender, RoutedEventArgs e)
        {
            int selected = components.SelectedIndex;
            XmlElement current = (XmlElement)components.Items.CurrentItem;
            componentsettings edit_cmpnt = new componentsettings(current);
            edit_cmpnt.cmpnt_mgr.manage_components += new manager.mngcmpnts(test);
            edit_cmpnt.Title = "Edit Component";
            edit_cmpnt.ShowDialog();
        }

        private void browsedir(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog directory;
            directory = new System.Windows.Forms.FolderBrowserDialog();
            directory.Description = "Please select the folder containing your g2 configuration (pxml) files. Default is C:\\Program Files\\Exacom, Inc.";
            directory.ShowDialog();
            startpath.Text = directory.SelectedPath;
        }

        private void save(object sender, RoutedEventArgs e)
         {
             MessageBox.Show(dp.Source.ToString());

            if(File.Exists(cfgfile + ".old"))
            {
                File.Delete(cfgfile + ".old");
            }

             File.Move(cfgfile, cfgfile + ".old");
             dp.Document.Save(cfgfile);
         }

        private void remove(object sender, RoutedEventArgs e)
        {
            XmlNode component = dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex];
            dp.Document.DocumentElement["Components"].RemoveChild(component);
        }

        private void temp(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex].InnerText.ToString()); //will this give me trouble????? Probably not since it's my config, but...
        }
        private void test(int id, XmlElement testelement)
        {

            //dp.Document.DocumentElement["Components"].ChildNodes[id] = testelement;
            //XmlNode testnode = dp.Document.DocumentElement["Components"].ChildNodes[id + 1];
            //XmlNode testnode = testelement;
            MessageBox.Show(testelement.OuterXml.ToString());
            //dp.Document.DocumentElement["Components"].InsertAfter(dp.Document.DocumentElement["Components"].ChildNodes[0], testelement);
            //dp.Document.DocumentElement["Components"].RemoveChild(testelement);
            //dp.Document.DocumentElement["Components"].ReplaceChild(testnode, dp.Document.DocumentElement["Components"].ChildNodes[id]);
            dp.Document.DocumentElement["Components"].PrependChild(testelement);

            //dp.Document.NodeChanged();

            //dp.Document.DocumentElement["Components"].CloneNode(true);
            dp.Document.DocumentElement["Components"].AppendChild(testelement);
            //dp.Document.
            //MessageBox.Show(dp.Document.OuterXml.ToString());
            //MessageBox.Show(dp.Document.DocumentElement["Components"].FirstChild.AppendChild(testelement).OuterXml.ToString());
        }


    }
}

JMSA. JMSA。 Thank you for the suggestion. 感谢您的建议。 I was playing around with it today and I found a solution. 我今天在玩它,并且找到了解决方案。 It's a little messy, but it works. 有点混乱,但是可以。 I copy one of the present nodes and then clear all of the values. 我复制当前节点之一,然后清除所有值。 Here is the code if anyone is interested. 如果有人感兴趣,这里是代码。

private void add(object sender, RoutedEventArgs e)
{
    System.Xml.XmlNode test = configfile.Document.DocumentElement["Components"].FirstChild;
    System.Xml.XmlNode testclone = test.Clone();
    for (int i = 0; i < testclone.ChildNodes.Count; i++)
    {
        testclone.ChildNodes[i].RemoveAll();
    }

    configfile.Document.DocumentElement["Components"].AppendChild(testclone);
    components.SelectedItem = components.Items.Count + 1;
}

My suggestion would be to create a class for each element by using a composite pattern, lazy loading the XML doc in the class and perform operations as you need. 我的建议是通过使用复合模式为每个元素创建一个类,将XML文档延迟加载到该类中,然后根据需要执行操作。 Then again save it. 然后再次保存。 Quite a handful though. 虽然不少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM