Textbox to XML & XML to Textbox

Marshal picture Marshal · Jan 25, 2012 · Viewed 7.7k times · Source

I have a program with a set of website links. These links could change at any time so I would like to be able to change the links during runtime and then save it to an XML file so when I close the program and open it again, it can load the settings from the XML file. I have managed to write to a text file the information I want to write however I am unsure how to replace a specific link and name and also call that specific link and name and link it to a button. The code below is to really just write to a XML document if it doesn't already exist. So I have not yet designed or written the form which will change an individual link.

private void button1_Click(object sender, EventArgs e)
{
    {
        XmlTextWriter writer = new XmlTextWriter("C:\\product.xml", System.Text.Encoding.UTF8);
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 2;
        writer.WriteStartElement("Table");
        createNode("1", "Product 1", "1000", writer);
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();
        MessageBox.Show("XML File created ! ");
    }
}

private void createNode(string Buttons, string Workshop_name, string URL, XmlTextWriter writer)
{
    writer.WriteStartElement("Buttons");
    writer.WriteStartElement("ButtonID");
    writer.WriteString(Buttons);
    writer.WriteEndElement();
    writer.WriteStartElement("Workshop_name");
    writer.WriteString(Workshop_name);
    writer.WriteEndElement();
    writer.WriteStartElement("URL");
    writer.WriteString(URL);
    writer.WriteEndElement();
    writer.WriteEndElement();
}

I want to be clear. I want to read one node from an XML document and write it to a textbox where a button can then link to the textbox. Since there will be many nodes with email addresses I want to know how to make the node I need unique to the other nodes.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
  <Buttons>
    <ButtonID>1</ButtonID>
    <Workshop_name>Cerner Training</Workshop_name>
    <URL>www.mlecerner.co.uk</URL>
  </Buttons>
  <Buttons>
    <ButtonID>2</ButtonID>
    <Workshop_name>Cerner Doctors</Workshop_name>
    <URL>www.cernerdoctors.co.uk</URL>
  </Buttons>
</Table>

Answer

Chuck Savage picture Chuck Savage · Jan 25, 2012

See my example here on reading/writing xml: https://stackoverflow.com/a/8899367/353147