Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Load XML element

Ive trying to make a program which needs to save settings to a file, usually I would use an ini equivelent file, however I have decided to move on and look at using an XML file instead, however all my googling I cannot find a way of just loading a particular element up from an xml file, everything seems to load each node up in a loop and you search inside it, which seems a bit daft to me.

What Id like to do is load an elements address up (sorry if thats not the right terminology) , for example if I had an xml file such as:-

<SETTINGS>
<Release>
<ProgName>Empire Burlesque</ProgName>
<Coder>Bob Dylan</Coder>
<Country>USA</Country>
</Release>
<Email>
<SMTPServer>mail.google.com</SMTPServer>
<SMTPPort>25</SMTPPort>
<MailFrom>someone@somewhere.com</MailFrom>
</Email>
<SETTINGS>

And I wanted to get the SMTP server, I thought I could do something like:-
			XmlDocument doc = new XmlDocument();
			doc.Load("settings.xml");

			XmlNode node = new xmlNode(doc);

			string smtpServer;
			smtpServer = node.search("SETTINGS\Email\SMTPServer");

Open in new window


And to write to an XML file, I thought:-
			XmlDocument doc = new XmlDocument();
			doc.Load("settings.xml");

			XmlNode node = new xmlNode(doc);
			node.search("SETTINGS\Email\SMTPServer") = "smtp.yahoo.com";

			doc.save();			

Open in new window


I know this is oversimplifying XML files by using them as a settings database, but if I want to store complex records Id use a database not an XML file.

I cant find any references on google for reading and writing one element like above, am I missing something here?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Fernando: your missing a using
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
Hi David;

You are correct I was missing a using statement. The only using statements that are needed for the code I posted are as follows.
using System.Xml.Linq;
using System.Xml.XPath;

Open in new window

the using statement
using System.Xml;

Open in new window

is not needed for this code.