I am new to C# and I'm creating a windows form application that has 2 TextBox controls and 1 RichTextBox.
I want it enable me to open an XML file and insert the values of a node inside the TextBox controls but most importantly I want to insert data in these boxes and update the value inside a node in an external XML file and save it.
This is what I have in terms of the form code.
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;
}
}
}
}
}
I'm trying to create a form to edit properties of twitter cards <meta>
tags. The problem so far is that it doesn't update the value in the XML file with what I inserted in the text box.
I don't quite understand your foreach loop, but binding the XmlElements you want to change to properties which update to/from the form controls is how I would approach it: