tags:

views:

670

answers:

4

Hi,

I have a XML File like that

<?xml version="1.0" encoding="utf-8" ?>

    <Configurations>

            <EmailConfiguration>
                <userName>xxxx</userName>
                <password>xxx</password>
                <displayName>xxxxx</displayName>
                <hostAddress>xxxx</hostAddress>
                <sslEnable>xxx</sslEnable>
                <port>xxx</port>
            </EmailConfiguration>

            <LogConfiguration>
                <logEnable>true</logEnable>
                <generalEnable>true</generalEnable>
                <warningEnable>true</warningEnable>
                <errorEnable>true</errorEnable>
            </LogConfiguration>

        </Configurations>

and I am using it as config file for my code and I want to retrieve their values (innerText) like that

bool logEnable = value comes from XML (logEnable)
bool warningEnable = value comes from XML (warningEnable)
bool errorEnable = value comes from XML (errorEnable)
bool generalEnable = value comes from XML (generalEnable)

So how can I read their values to assign them to the boolean variables and if I wanted to change one of their values with false, How would I be able to do that ?

Thanks...

Regards...

P.s : If you wrote more explanatory codes, It would be so much appreciated.

Thanks again...

A: 

Is this a web.config file? The answer changes based on that.

jcollum
It's a normal xml file.
Braveyard
+3  A: 
public class Options
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string DisplayName { get; set; }
    public string HostAddress { get; set; }
    public bool SSL { get; set; }
    public string Port { get; set; }

    public bool LogEnable { get; set; }
    public bool GeneralEnable { get; set; }
    public bool WarningEnable { get; set; }
    public bool ErrorEnable { get; set; }

    public static Options Load(string path)
    {
        Options options = new Options();
        XmlDocument xml = new XmlDocument();
        xml.Load(path);

        XmlNodeReader input = new XmlNodeReader(xml);

        while (input.Read())
        {
            var elementname = input.Name.ToLower();

            switch (elementname)
            {
                case "username":
                    options.UserName = input.Value;
                    break;
                // all other cases
                case "logenable":
                    options.LogEnable = Boolean.Parse(input.Value);
                    break;
                // continue with other cases
            }
        }
    }

    public static void Save(Options options, string path)
    {
        XmlTextWriter writer = new XmlTextWriter(path);

        xmlWriter.WriteStartDocument(true);
        xmlWriter.WriteStartElement("configuration");
        xmlWriter.WriteStartElement("emailConfiguration");

        xmlWriter.WriteStartElement("userName");
        xmlWriter.WriteString(options.UserName);
        xmlWriter.WriteEndElemement();

        // continue for all elements

        xmlWriter.WriteEndElement();
        xmlWriter.WriteStartElement("logConfiguration");

        xmlWriter.WriteStartElement("logEnable");
        xmlWriter.WriteString(options.LogEnable.ToString());
        xmlWriter.WriteEndElemement();

        // continue for all elements

        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();

        xmlWriter.Close();
    }
}

I left some work for you to complete ;) Also, I didn't write this is Visual Studio, and I didn't compile it before hand. This code is provided as is with no guarantee or warranty. ;)

This is basic XML Read/Write process in .NET, though there are many options. You could use XPath queries, or if you are using .NET 3.5 you could use Linq to Sql which will give you street cred with the cool kids. But the above sample should get you up and running quickly, just promise you'll go do some research on these other things too, you'll be all the better for it.

NerdFury
Thanks, I am gonna do some research right now :) thanks for helping.
Braveyard
+1  A: 

Best practice would be to actually use the web.config or the app.config file. It also makes this process a whole lot easier.

Ironsides
Yes, he could litter his appSettings with these values, it is true that it is easier. Creating a custom configuration section is not easier. But his basic question was on reading and writing XML files. Not should he use XML files. And reading and writing XML is a good thing to understand.
NerdFury
Maybe but when I change any value of them, they said that I needed to restart the application so it should be a problem later.
Braveyard
if you change the settings while the application is running all you have to do is refresh the app.config. if i remember it is something like refreshsection.applicationsettings i just used this technigue in a video player authoring application
Ironsides
A: 

I've found something like this and this actually satisfied me how I want :) But I wanna learn that is a efficient way to retrieve data from XML ? Here is the code tho :

XmlDocument doc = new XmlDocument();

            doc.Load(HttpContext.Current.Server.MapPath("config.xml"));

            logEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["logEnable"].InnerText);
            warningEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["warningEnable"].InnerText);
            errorEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["errorEnable"].InnerText);
            generalEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["generalEnable"].InnerText);
Braveyard