Solved

Editing a single value in XML file from a windows Form application c#

Posted on 2010-08-19
4
459 Views
Last Modified: 2013-12-17
Hi,

Am using VS2008, I've a c# windows form app and I am loading info from my XML file into various text boxes on the form. (this bit is working) I need to be able to edit one of the text boxes and have this change updated to the XML file. I've had a go at this but, the file doesn't change. (I'm very new to xml, 1 day) My XML file format is similar to the following:


<?xml version="1.0" encoding="utf-8"?>
<Lessons>
  <Lesson ID =" 01">
    <ButtonName>btnLesson01</ButtonName>
    <Title>Lesson One</Title>
    <Description>This will recreate etc etc </Description>
    <Points>1</Points>
  </Lesson>
  <Lesson ID =" 02">
    <ButtonName>btnLesson02</ButtonName>
    <Title>Lesson Two</Title>
    <Description>This will etc etc </Description>
    <Points>9</Points>
  </Lesson>
  <Lesson ID =" 03">
    <ButtonName>btnLesson03</ButtonName>
   <Title>Lesson Three</Title>
    <Description>This will etc etc </Description>
    <Points>4</Points>
  </Lesson>
  <Lesson ID ="04">
    <ButtonName>btnLesson04</ButtonName>
   <Title>Lesson Four</Title>
    <Description>This will etc etc </Description>
    <Points>10</Points>
  </Lesson>
</Lessons>



My attached code isn't updating the Points element though.
lessonID   in   XmlNode nodeToEdit = doc.SelectSingleNode("/Lessons/Lesson[@ID="+lessonID+"]");    is a string which I assign a value to elsewhere.

Any idea? Thanks





XmlReader reader = XmlReader.Create(FILE_NAME);

            XmlDocument doc = new XmlDocument();

            doc.Load(reader);

       

            XmlNode nodeToEdit = doc.SelectSingleNode("/Lessons/Lesson[@ID="+lessonID+"]");

              if (nodeToEdit != null)

              {

                  nodeToEdit["Points"].InnerText = txtPoints.Text;

                  

              }

              doc.Save("FILE_NAME");

              doc = null;

Open in new window

0
Comment
Question by:gemky
4 Comments
 
LVL 5

Accepted Solution

by:
tvPrasad earned 250 total points
ID: 33477918
Hi,
Are you supplying a valid path file name in save ?
 You should supply path i.e.. C:\Lessons.xml
  doc.Save(@"C:\Lessons.xml");

You can check the xml file for updates once executed.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 33477976
Hi gemky;

I tested your code without modifying it and it works fine, are you sure it is not updating the correct Lesson ID node, are you looking at the wrong Lesson ID?

Fernando
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33478161
0
 

Author Closing Comment

by:gemky
ID: 33483703
Thanks for your help.   I didn't realise this was actually saving as I was only looking at Lessons.xml in the directory. I hadn't spotted a new file called FILENAME in the directory.

What I was doing was silly, I have

 const string FILE_NAME = "Lessons.xml";

and in the save I had
doc.Save("FILE_NAME");  

doc.Save(FILE_NAME);

Thanks everyone
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now