[英]C# Windows Form Application - value not printing in textbox for each time
[英]windows form, how to edit each node value in xml file by the the value in the textbox in the form. c#
我是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文件中的值。
我不太了解您的foreach循環,但是將要更改的XmlElement綁定到更新到表單控件或從表單控件更新的屬性是我的處理方式:
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));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.