Reputation: 835
I'm working on a legacy application that uses a static class to store settings that are read from a custom XML file.
However, as part of a slight upgrade to the module, the customer would like to see, at runtime, which fields are missing.
Consider the following Settings.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<configuration>
<API>
<log>
<type>09</type>
<location>C:\Test\Test.log</filename>
</log>
</API>
</configuration>
</appSettings>
The settings are currently read into the static class using an XMLReader (seen below):
using (XmlTextReader xmlReader = new XmlTextReader("Settings.xml"))
{
xmlReader.ReadToFollowing("API");
xmlReader.ReadToFollowing("log");
xmlReader.ReadToFollowing("type");
this.logtype = xmlReader.ReadElementContentAsString();
//snip...
}
This same code is used to read each and every setting. There has to be a better way. Is there any way that I can read the XML values into each corresponding property, and generate an error if it's null?
I'm attempting to design the static Settings class as such:
public static class Settings
{
private static string logtype
public static string LogType
{
get
{ return logtype; }
set
{ logtype = value; }
}
}
And then use something like the following to "grab" the values:
public static void initSettings()
{
appSettings.LogType = read the configuration\API\log\type field from xml;
}
I'm pretty sure that I'd just check for the null character in the property constructor, but how would I do the 'read the configuration\API\log\type field from xml' part of the initSettings method?
Upvotes: 0
Views: 1255
Reputation: 1931
You could use XMLDocument / XPath. Something like this:
XmlDocument doc = new XmlDocument();
doc.Load("Settings.xml");
appSettings.LogType = doc.SelectSingleNode("/appSettings/configuration/API/log/type").InnerText;
Actually I would prefer to use Serialization. Maybe the standard serialization a YAXLib, which provides some useful error handling.
Edit: For me it's working with InnerText.
Upvotes: 0
Reputation: 32807
Use LINQ2XML..Its COOL
XELement doc=XElement.Load("yourXML.xml");
var LogList=doc.Descendants("configuration").Descendants("API").Descendants("log")
.Select(x=>
new
{
type=x.Element("type").Value;
location=x.Element("location").Value;
}
);
Now you can use for-each loop to access each and every log in logList
foreach(var log in logList)
{
Console.WriteLine(log.type);
Console.WriteLine(log.location);
}
Also you can check if its empty
if(log.type==""){//its empty}
Upvotes: 1
Reputation: 449
I prefer to use XmlNodeList personally.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Settings.xml");
// Get element from xml.
XmlNodeList type = xmlDoc.GetElementsByTagName("type");
// The following line assumes that there is only 1 type entry
appSettings.LogType = type[0].InnerText;
If there are multiple entries you want to get, just slap in a for statement.
for (int i = 0; i < type.Count; i++)
{
// Do stuff
appSettings.LogType = type[i].InnerText;
}
Upvotes: 0