C# Read single value from simple XML

XML

<Config>  <Source_Directory>C:inetpubwwwroot</Source_Directory>  <Backup_Destination>C:Backup_Test</Backup_Destination>  <Filter>*config*</Filter>  <Archive>true</Archive>  <DeleteUncompressedCopy>true</DeleteUncompressedCopy></Config>

C#

public bool ConfigFileExists
{
    get
    {
        if (!string.IsNullOrEmpty(CONFIG_FILE))
        {
            if (System.IO.File.Exists(CONFIG_FILE))
            {
                return true;
            }
        }
        return false;
    }
}

private string GetConfigData(string propertyName)
{
    if (ConfigFileExists)
    {
        XElement xml = XElement.Load(CONFIG_FILE);
        return xml.Element(propertyName).Value;
    }
    return string.Empty;
}

Usage

private string _source;
public string Source
{
    get
    {
        _source =  GetConfigData(“Source_Directory”);
        return _source;
    }
    set
    {
        _source = value;
    }
}
private string _destination;
public string Destination
{
    get
    {
        _destination = GetConfigData(“Backup_Destination”);
        return _destination;
    }
    set
    {
        _destination = value;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *