471,306 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

modification of XML file

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...b;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.

Feb 7 '06 #1
4 2064
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


"Vincent" wrote:
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...b;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.

Feb 7 '06 #2
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.

Feb 7 '06 #3
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.

"Vincent" wrote:
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.

Feb 7 '06 #4
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!

Feb 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by nathan_kent_bullock | last post: by
3 posts views Thread by stan | last post: by
13 posts views Thread by ts-dev | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.