1

我是 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;
                }

            }
        }
    }
}

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

4

1 回答 1

0

我不太了解您的 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));
         }
     }
}
于 2015-11-23T14:22:30.743 回答