Read settings from an xml in a class library

I have a class library project. I have certain configuration settings stored in an xml file. Is there anyway I could read the settings from the xml file. If yes how can I  go about doing it.
LVL 1
pmac38CDSAsked:
Who is Participating?
 
käµfm³d 👽Commented:
There are several options in .NET. There are several classes under the System.Xml namespace if you are using .NET >= 3.5, then you can also use Linq. Here are a few examples:

Example Settings File
<?xml version="1.0" encoding="utf-8" ?>
<Setting>
  <Setting1>Hello</Setting1>
  <Setting2>World</Setting2>
  <Setting3>!</Setting3>
</Setting>

Open in new window



XmlDocument Example
static TheSettings GetSettingsByXmlDocument(string filePath)
{
    TheSettings settings = new TheSettings();
    XmlDocument xdoc = new XmlDocument();
    XmlNode temp;

    xdoc.Load(filePath);

    temp = xdoc.SelectSingleNode("/Settings/Setting/Setting1");

    if (temp != null) settings.Setting1 = temp.InnerText;

    temp = xdoc.SelectSingleNode("/Settings/Setting/Setting2");

    if (temp != null) settings.Setting2 = temp.InnerText;

    temp = xdoc.SelectSingleNode("/Settings/Setting/Setting3");

    if (temp != null) settings.Setting3 = temp.InnerText;

    return settings;
}

Open in new window


Linqt Example
static TheSettings GetSettingsByLinq(string filePath)
{
    TheSettings settings = new TheSettings();
    XDocument xdoc = XDocument.Load(filePath);
    XElement temp;

    temp = xdoc.Descendants("Setting1").FirstOrDefault();

    if (temp != null) settings.Setting1 = temp.Value;

    temp = xdoc.Descendants("Setting2").FirstOrDefault();

    if (temp != null) settings.Setting2 = temp.Value;

    temp = xdoc.Descendants("Setting3").FirstOrDefault();

    if (temp != null) settings.Setting3 = temp.Value;

    return settings;
}

Open in new window


Serialization Example
static TheSettings GetSettingsBySerialization(string filePath)
{
    TheSettings settings;
    XmlSerializer deserializer = new XmlSerializer(typeof(TheSettings));

    try
    {
        settings = deserializer.Deserialize(XmlTextReader.Create(filePath)) as TheSettings;
    }
    catch
    {
        settings = null;
    }

    return settings;
}

Open in new window


Example Usage
static void Main(string[] args)
{
    TheSettings linqExample = GetSettingsByLinq("MySettings.xml");
    TheSettings xmlDocExample = GetSettingsByXmlDocument("MySettings.xml");
    TheSettings serializationExample = GetSettingsBySerialization("MySettings.xml");

    System.Diagnostics.Debugger.Break();
}

Open in new window

0
 
käµfm³d 👽Commented:
Ooops! Forgot to mention one detail. You'll need the following namespaces accordingly:


XmlDocument Example
using System.Xml;

Open in new window



Linq Example
using System.Xml.Linq;

Open in new window



Serialization Example
using System.Xml.Serialization;

Open in new window



Also, here is the definition of my "TheSettings" class. The XmlRoot attribute is only necessary for the Serialization example.
[XmlRoot(ElementName = "Setting")]  // Because "TheSetting" doesn't match the name of the node in the XML
public class TheSettings
{
    public string Setting1 { get; set; }
    public string Setting2 { get; set; }
    public string Setting3 { get; set; }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.