modification of XML file

V

Vincent

Hi!

I'm quite new to C#, and I want to edit an XML file. I followed the
HOW-TO here :
http://support.microsoft.com/default.aspx?scid=kb;en-us;317666 but it
didn't help that much. I work under Linux and MonoDevelop. The file I
want to modify is here : http://pastebin.com/542434
Let's say that I want to edit the "mtime" field. I use this piece of
code : http://pastebin.com/542437
Actually, I don't get any compilation error, but I don't get any
modification as well... Does somedy see an error somewhere in the code
?

Thanks in advance for your help!
Vincent.
 
G

Guest

Hi Vincent,
a couple of points pop out:

1. You are not loading any data into the XmlDocument object, you need to add
a line like:
xmlDoc.Load(txtReader);

2. Your XPath query is not correct, instead of being "entry" it should be
"/gconf/entry"

3. You ar enot saving your changes back to file, you are only modifying
them in memory.

Below is some code that is a modified version of what you posted, it should
work for you:


XmlDocument xmlDoc = new XmlDocument();
String path = "background.xml";

xmlDoc.Load(path);
XmlNodeList nodeList = xmlDoc.SelectNodes("/gconf/entry");

foreach (XmlNode node in nodeList)
{
node.Attributes["mtime"].Value =
(Int32.Parse(node.Attributes["mtime"].Value) + 1).ToString();
}

xmlDoc.Save("background.xml");

Hope that helps
Mark Dawson
http://www.markdawson.org
 
V

Vincent

Thank you Mark, it works perfectly well! Sorry to have wasted your
time, I'm a real beginner (and a little stupid!) as you can see ;)
Thank you again.
Vincent.
 
G

Guest

Hi Vincent,
no need to be sorry, we all were beginners at some point :) Just keep
plugging away and in no time you will be flying through the code.

Mark.
 
V

Vincent

Hi again,

What if I want to modify the values between the "stringvalues" tags ? I
tried to do with the help of the nodeList, but it seems that it's not
the good way to do it... BTW, maybe you know a good c#/xml tutorial
that i can use insted of bothering you ;)

Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top