13

First of all this is C#. I am creating a internet dashboard for a small group of colleages in the NHS. Below is an example xml file in which I need to change the innertext of. I need to replace a specific element for example "Workshop1." Because we have a few workshops I cannot afford to use a general writer because it will replace all the information on the XML document with this one bit of code below.

<?xml version="1.0" ?> 
   <buttons>
      <workshop1>hello</workshop1> 
      <url1>www.google.co.uk</url1> 

I am using a switch case to select a specific workshop where you can change the name and add a URL of the workshop and using this code below will replace the whole document.

public void XMLW()
    {
        XmlTextReader reader = new XmlTextReader("C:\\myXmFile.xml");
        XmlDocument doc = new XmlDocument(); 

        switch (comboBox1.Text)

        {
            case "button1":


                doc.Load(reader); //Assuming reader is your XmlReader 
                doc.SelectSingleNode("buttons/workshop1").InnerText = textBox1.Text;
                reader.Close();
                doc.Save(@"C:\myXmFile.xml");
                break;


        }


    }

So just to clarify I want my C# program to search through the XML document find the element "Workshop1" and replace the innertext with text from a textBox. and be able to save it without replacing the whole document with one node. Thanks for looking.

Marshal
  • 1,177
  • 4
  • 18
  • 30
  • 2
    Is there a reason why you aren't using the `XDocument` or at least the `XmlDocument` API? You normally only use `XmlReader` / `XmlWriter` if you have to process *huge* XML files. – Daniel Hilgarth Feb 07 '12 at 10:17
  • Well I am a bit of a newbie in C#. So to get the general program running I am bashing code together and then when I can see it working I can change the code to make it more efficient. Atleast this way I can learn the difference between ugly and clean ways of doing things and understand the advantages of both. However thank you for your guidance I will look to use XMLReader/XMLWriter now. – Marshal Feb 07 '12 at 11:20
  • My advice was to NOT use `XmlReader` / `XmlWriter`... – Daniel Hilgarth Feb 07 '12 at 11:52

1 Answers1

17

Using XmlDocument and XPath you can do this

XmlDocument doc = new XmlDocument();
doc.Load(reader); //Assuming reader is your XmlReader
doc.SelectSingleNode("buttons/workshop1").InnerText = "new text";

You can use doc.Save to save the file also.

Read more about XmlDocument on MSDN.

EDIT

To save the document do this

doc.Save(@"C:\myXmFile.xml"); //This will save the changes to the file.

Hope this helps you.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • I think its working. The only issue now is it won't let me save the doc. I am using the "doc.Save;" However it's coming up with an error **"Only assignment, call, increment, decrement, and new object expressions can be used as a statement."** Is this because I am not using an XMLWriter? or? I have been working on this one program for so long it's probably me being an idiot. If you look above you can see it ammended. – Marshal Feb 07 '12 at 11:12
  • Edited the code above to show now that it's working and for future users as a reference. Thanks to Amar Palsapure. – Marshal Feb 07 '12 at 11:32
  • Its great when things come together and run smoothly :) – Amar Palsapure Feb 07 '12 at 11:34