By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

Reading in XML file using C#

P: n/a
Hi

Reading in XML file using C#, changing elements and saving back to the
file, how can i do that ? I want to be able to read an XML file and
read out Elements. for example

<name>Tom</tom>

When I read from a file i want to save the XML document in memory of
the computer and when i am finished flush it back to the file, that
means i want to be able to access the element in memory of the
computer and when i change it will write it back with new changes.

What is the Best way to do that ?
I have tried it wiht xmlReader and xmlWrite, this is not saved in the
computer.
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi

When you want to read the XML file to memory, use:

XmlDataDocument myDoc = new XmlDataDocument();
myDoc.Load( <filename>);

Then work with the DataDoc as normal to manipulate your data.

When you want to save the File back to the HDD, use normal File I/O to
overwrite your existing file. Get the file's contents from the
XmlDataDocument's OuterXml property...

HTH

Cobus Lombard
Application Developer
Mint Net
South Africa

"Gerald Maher" <ge**********@waytohere.com> wrote in message
news:d0**************************@posting.google.c om...
Hi

Reading in XML file using C#, changing elements and saving back to the
file, how can i do that ? I want to be able to read an XML file and
read out Elements. for example

<name>Tom</tom>

When I read from a file i want to save the XML document in memory of
the computer and when i am finished flush it back to the file, that
means i want to be able to access the element in memory of the
computer and when i change it will write it back with new changes.

What is the Best way to do that ?
I have tried it wiht xmlReader and xmlWrite, this is not saved in the
computer.

Nov 15 '05 #2

P: n/a
Hey,

I would just use a standard XmlDocument to do what your looking to do:

XmlDocument myDoc = new XmlDocument();
myDoc.Load(myfile.xml);
myDoc.SelectSingleNode("//username[@id=2]/username").Value = "myfirstpassword";
myDoc.Save(myfile.xml);

myfile.xml
----------------
<?xml version="1.0" encoding="utf-8" ?>
<data>
<user id="2">
<username>tom</username>
<password>default</password>
</user>
</data>

"Cobus" <co*****@hotmail.com> wrote in message news:<uu**************@TK2MSFTNGP11.phx.gbl>...
Hi

When you want to read the XML file to memory, use:

XmlDataDocument myDoc = new XmlDataDocument();
myDoc.Load( <filename>);

Then work with the DataDoc as normal to manipulate your data.

When you want to save the File back to the HDD, use normal File I/O to
overwrite your existing file. Get the file's contents from the
XmlDataDocument's OuterXml property...

HTH

Cobus Lombard
Application Developer
Mint Net
South Africa

"Gerald Maher" <ge**********@waytohere.com> wrote in message
news:d0**************************@posting.google.c om...
Hi

Reading in XML file using C#, changing elements and saving back to the
file, how can i do that ? I want to be able to read an XML file and
read out Elements. for example

<name>Tom</tom>

When I read from a file i want to save the XML document in memory of
the computer and when i am finished flush it back to the file, that
means i want to be able to access the element in memory of the
computer and when i change it will write it back with new changes.

What is the Best way to do that ?
I have tried it wiht xmlReader and xmlWrite, this is not saved in the
computer.

Nov 15 '05 #3

P: n/a
bd******@hotmail.com (Ben Dewey) wrote in message news:<a7*************************@posting.google.c om>...
Hey,

I would just use a standard XmlDocument to do what your looking to do:

XmlDocument myDoc = new XmlDocument();
myDoc.Load(myfile.xml);
myDoc.SelectSingleNode("//username[@id=2]/username").Value = "myfirstpassword";
myDoc.Save(myfile.xml);

myfile.xml
----------------
<?xml version="1.0" encoding="utf-8" ?>
<data>
<user id="2">
<username>tom</username>
<password>default</password>
</user>
</data>

How can can I add a new node say :
<username>Peter</username>
<password></password>

in C# code
Nov 15 '05 #4

P: n/a
"Cobus" <co*****@hotmail.com> wrote in message news:<uu**************@TK2MSFTNGP11.phx.gbl>...
Hi

When you want to read the XML file to memory, use:

XmlDataDocument myDoc = new XmlDataDocument();
myDoc.Load( <filename>);

Then work with the DataDoc as normal to manipulate your data.

When you want to save the File back to the HDD, use normal File I/O to
overwrite your existing file. Get the file's contents from the
XmlDataDocument's OuterXml property...

HTH

Cobus Lombard
Application Developer
Mint Net
South Africa

How can i use this if 2 users on different computers using the same file ?

i.e. how can i protect 2 people writing over the same file ?
Nov 15 '05 #5

P: n/a
Have a look at source/version control software too.

CVS is capable and free. Bit of a pain barrier if you havent used these
tools before but they are very good at what they do, i.e. booking and
catalogue system that prevents overwrites as you suggest.

http://www.cvshome.org/

Regards

Tam Inglis
"Gerald Maher" <ge**********@waytohere.com> wrote in message
news:d0**************************@posting.google.c om...
"Cobus" <co*****@hotmail.com> wrote in message

news:<uu**************@TK2MSFTNGP11.phx.gbl>...
Hi

When you want to read the XML file to memory, use:

XmlDataDocument myDoc = new XmlDataDocument();
myDoc.Load( <filename>);

Then work with the DataDoc as normal to manipulate your data.

When you want to save the File back to the HDD, use normal File I/O to
overwrite your existing file. Get the file's contents from the
XmlDataDocument's OuterXml property...

HTH

Cobus Lombard
Application Developer
Mint Net
South Africa

How can i use this if 2 users on different computers using the same file ?

i.e. how can i protect 2 people writing over the same file ?

Nov 15 '05 #6

P: n/a
"Tam Inglis" <jo***********@btclick.com> wrote in message news:<br**********@sparta.btinternet.com>...
Have a look at source/version control software too.

CVS is capable and free. Bit of a pain barrier if you havent used these
tools before but they are very good at what they do, i.e. booking and
catalogue system that prevents overwrites as you suggest.

http://www.cvshome.org/

Regards

Tam Inglis
"Gerald Maher" <ge**********@waytohere.com> wrote in message
news:d0**************************@posting.google.c om...
"Cobus" <co*****@hotmail.com> wrote in message

news:<uu**************@TK2MSFTNGP11.phx.gbl>...
Hi

When you want to read the XML file to memory, use:

XmlDataDocument myDoc = new XmlDataDocument();
myDoc.Load( <filename>);

Then work with the DataDoc as normal to manipulate your data.

When you want to save the File back to the HDD, use normal File I/O to
overwrite your existing file. Get the file's contents from the
XmlDataDocument's OuterXml property...

HTH

Cobus Lombard
Application Developer
Mint Net
South Africa

How can i use this if 2 users on different computers using the same file ?

i.e. how can i protect 2 people writing over the same file ?


???

How does this help ? how can i access 2 files with 2 different computers
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.