0

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>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marshal
  • 1,177
  • 4
  • 18
  • 30
  • You shouldn't use `XmlTextWriter` directly, and you should enclose it in a `using` block: `using (XmlWriter writer = XmlWriter.Create("C:\\product.xml")){...}` – John Saunders Jan 25 '12 at 12:10

1 Answers1

0

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

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • Thank you very much. I would run it but come into a snag. "ReplaceWith" comes up with an error. Am I missing a reference? 'System.Xml.Linq.XAttribute' does not contain a definition for 'ReplaceWith' and no extension method 'ReplaceWith' accepting a first argument of type 'System.Xml.Linq.XAttribute' could be found (are you missing a using directive or an assembly reference? – Marshal Jan 26 '12 at 10:28
  • Good catch, guess I'd written my own. I added it to the example I linked. – Chuck Savage Jan 27 '12 at 01:10