Working with XML files
Parse XML files
var xmlFilePath = @"C:\TestData\site1.xml";
if (!System.IO.File.Exists(xmlFilePath))
{
Toolbox.Log(string.Format("Xml file not found: {0}", xmlFilePath), LogLevel.Error);
return;
}
// One way of loading XML file
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
// Get root node
XmlNode root = doc.SelectSingleNode("Site");
if (root.HasChildNodes)
{
// Traverse child nodes
for (int i = 0; i < root.ChildNodes.Count; i++)
{
var childNode = root.ChildNodes[i];
switch (childNode.Name)
{
case "AirDensityReference":
Workbook.Climate.AirDensityModel.ReferenceAirDensity =
double.Parse(childNode.InnerText);
break;
case "ReferenceHeight":
Workbook.Climate.AirDensityModel.ReferenceElevation =
double.Parse(childNode.InnerText);
break;
}
}
}
// Second method of accessing XML files, using XPath. Selects nodes using their path
// in the xml file
var node = doc.SelectSingleNode("/Site/AirDensityReference");
if (node != null)
{
Workbook.Climate.AirDensityModel.ReferenceAirDensity = double.Parse(node.InnerText);
}
node = doc.SelectSingleNode("/Site/ReferenceHeight");
if (node != null)
{
Workbook.Climate.AirDensityModel.ReferenceElevation = double.Parse(node.InnerText);
}