Reading in XML file using C#

  • Thread starter Thread starter Gerald Maher
  • Start date Start date
G

Gerald Maher

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.
 
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
 
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>
 
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
 
Cobus said:
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 ?
 
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
 
Tam Inglis said:
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

???

How does this help ? how can i access 2 files with 2 different computers
 
Back
Top