windows窗体中,如何根据.c#窗体中文本框中的值编辑xml文件中的每个节点值

本文关键字:窗体 编辑 xml 节点 文件 文本 何根 windows 中文 | 更新日期: 2023-09-27 18:24:05

我是C#的新手,我正在创建一个具有2个TextBox控件和1个RichTextBox的windows窗体应用程序。

我希望它能让我打开一个XML文件,并在TextBox控件中插入节点的值,但最重要的是,我想在这些框中插入数据,并在外部XML文件中更新节点内的值并保存它。

这就是我的表单代码。

namespace WindowsForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button4.Enabled = true;
        }
        XmlDocument xDoc;
        string path;
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            xDoc.Save(path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
           xDoc.SelectSingleNode("TwitterCards/Card1/title").InnerText = textBox1.Text;
           xDoc.SelectSingleNode("TwitterCards/Card1/image").InnerText = textBox2.Text;
           xDoc.SelectSingleNode("TwitterCards/Card1/description").InnerText = richTextBox1.Text; xDoc.Save(path);

        }
        XmlDocument xDoc1;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void button3_Click(object sender, EventArgs e)
        {
        }
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
                xDoc = new XmlDocument();
                xDoc.Load(path);
                XmlNodeList nodeList = xDoc.DocumentElement.SelectNodes("TwitterCards/Card1");
                string title = "", image = "", description = "";
                foreach (XmlNode node in nodeList)
                {
                    title = node.SelectSingleNode("title").InnerText;
                    image = node.SelectSingleNode("image").InnerText;
                    description = node.SelectSingleNode("description").InnerText;
                    textBox1.Text = (title);
                    textBox2.Text = (image);
                    richTextBox1.Text = (description);
              //  }
                 textBox1.Text = xDoc.SelectSingleNode("TwitterCards/Card1/title").InnerText;
                 textBox2.Text = xDoc.SelectSingleNode("TwitterCards/Card1/image").InnerText;
                 richTextBox1.Text = xDoc.SelectSingleNode("TwitterCards/Card1/description").InnerText;
                }
            }
        }
    }
}

我正在尝试创建一个表单来编辑twitter卡<meta>标签的属性。到目前为止的问题是,它没有用我在文本框中插入的内容更新XML文件中的值。

windows窗体中,如何根据.c#窗体中文本框中的值编辑xml文件中的每个节点值

我不太理解foreach循环,但将要更改的XmlElements绑定到从表单控件更新的属性是我处理它的方法:

test.xml:
<?xml version="1.0" encoding="utf-8"?>
<TwitterCards>
  <Card1>
    <Site> @_Paul</Site>
    <title> Schneider's </title>
    <image> "C:''AAA.jpg" </image>
    <description>foo</description>
  </Card1>
</TwitterCards>
public partial class Form1 : Form, INotifyPropertyChanged
{
    XmlDocument doc = new XmlDocument();
    XmlElement m_textElem1;
    XmlElement m_textElem2;
    XmlElement m_textElem3;
    public string TextElement1Content
    {
        get { return m_textElem1.InnerText; }
        set { m_textElem1.InnerText = value;  }
    }
    public string TextElement2Content
    {
        get { return m_textElem2.InnerText; }
        set { m_textElem2.InnerText = value; }
    }
    public string TextElement3Content
    {
        get { return m_textElem3.InnerText; }
        set { m_textElem3.InnerText = value; }
    }

    public Form1()
    {
        InitializeComponent();
        doc.Load("..''..''test.xml");
        m_textElem1 = doc.SelectSingleNode("TwitterCards/Card1/title") as XmlElement;
        m_textElem2 = doc.SelectSingleNode("TwitterCards/Card1/image") as XmlElement;
        m_textElem3 = doc.SelectSingleNode("TwitterCards/Card1/description") as XmlElement;
        textBox1.DataBindings.Add("Text", this, "TextElement1Content");
        textBox2.DataBindings.Add("Text", this, "TextElement2Content");
        richTextBox1.DataBindings.Add("Text", this, "TextElement3Content");
    }
     private void saveButton_Click(object sender, EventArgs e)
     {
         doc.Save("..''..''saved.xml");
     }
     public event PropertyChangedEventHandler PropertyChanged;
     public void NotifyPropertyChanged(string propName)
     {
         if (PropertyChanged != null )
         {
             PropertyChanged(this, new PropertyChangedEventArgs(propName));
         }
     }
}