gemky
asked on
Editing a single value in XML file from a windows Form application c#
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</B uttonName>
<Title>Lesson One</Title>
<Description>This will recreate etc etc </Description>
<Points>1</Points>
</Lesson>
<Lesson ID =" 02">
<ButtonName>btnLesson02</B uttonName>
<Title>Lesson Two</Title>
<Description>This will etc etc </Description>
<Points>9</Points>
</Lesson>
<Lesson ID =" 03">
<ButtonName>btnLesson03</B uttonName>
<Title>Lesson Three</Title>
<Description>This will etc etc </Description>
<Points>4</Points>
</Lesson>
<Lesson ID ="04">
<ButtonName>btnLesson04</B uttonName>
<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("/Les sons/Lesso n[@ID="+le ssonID+"]" ); is a string which I assign a value to elsewhere.
Any idea? Thanks
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</B
<Title>Lesson One</Title>
<Description>This will recreate etc etc </Description>
<Points>1</Points>
</Lesson>
<Lesson ID =" 02">
<ButtonName>btnLesson02</B
<Title>Lesson Two</Title>
<Description>This will etc etc </Description>
<Points>9</Points>
</Lesson>
<Lesson ID =" 03">
<ButtonName>btnLesson03</B
<Title>Lesson Three</Title>
<Description>This will etc etc </Description>
<Points>4</Points>
</Lesson>
<Lesson ID ="04">
<ButtonName>btnLesson04</B
<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("/Les
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I just resolved another very similar issue here:https://www.experts-exchange.com/questions/26414327/vb-net-XML-edit-in-form.html
ASKER
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
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