I'm trying to write a code for getting element from .config file in this format:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<alias alias="hierarchical" type="Microsoft.Practices.Unity.HierarchicalLifetimeManager, Microsoft.Practices.Unity" />
<alias alias="session" type="Microsoft.Practices.Unity.SessionLifetimeManager, TelventDMS.Web.Common" />
<alias alias="IReportService" type="Web.Common.Interfaces.IReportService, Web.Common" />
<alias alias="ReportServiceImpl" type="Web.Common.Services.ReportServiceImpl`1, Web.Common" />
<alias alias="TAReport" type="Web.WebClient.Areas.Reports.Services.TopologyAnalyzerServiceImpl, Web.WebClient" />
<alias alias="TAReportJobParam" type="UI.ServiceProxies.TAReportJobParam, UI.ServiceProxies.ServiceProxies" />
<alias alias="ViolationsReport" type="Web.WebClient.Areas.Reports.Services.ViolationsServiceImpl, Web.WebClient.TDMSWebApp" />
<alias alias="ViolationsJobParam" type="UI.ServiceProxies.ViolationsJobParam, UI.ServiceProxies.ServiceProxies" />
<assembly name="Web.WebClient.TDMSWebApp" />
<container name="container">
<register name="configService" type="Web.Common.Interfaces.IConfigService, Web.Common"
mapTo="Web.Common.Services.ConfigServiceImpl, Web.Common">
<lifetime type="singleton" />
<constructor>
<param name="res" value="Resources.ClientStrings"> </param>
<param name="configFile" value="webclient.config"> </param>
</constructor>
</register>
<register name="scaleCoefConfigService" type="Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp"
mapTo="Web.WebClient.Services.Implementations.ScaleCoefConfigServiceImpl, Web.WebClient.TDMSWebApp">
<lifetime type="singleton" />
<constructor>
<param name="configService">
<dependency name="configService"/>
</param>
</constructor>
</register>
<register name="sessionService" type="Web.Common.Interfaces.ISessionService, Web.Common"
mapTo="Web.Common.Services.SessionServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
<register name="licenseManagerService" type="Web.Common.Interfaces.ILicenseManagementService, Web.Common"
mapTo="Web.Common.Services.LicenseManagementServiceImpl, Web.Common">
<lifetime type="singleton" />
</register>
</container>
</unity>
</configuration>
After I get registers I want to put values of register types and mapTo in separated lists, with this code:
private void ReadAdvancedConfigFile()
{
XElement root = null;
root = XElement.Load(new XmlTextReader(@"C:\Users\nemanja.mosorinski\Downloads\__Research-master\__Research-master\SEDMSVSPackage\VisualStudioPackage\AppRes\ConfigFiles\Unity.config"));
if (root != null)
{
var registers = root.Element("unity").Element("container").Descendants("register");
List<string> tipList = new List<string>();
List<string> mapToList = new List<string>();
if (registers.Count() > 0)
{
foreach (var reg in registers)
{
tipList.Add(root.Attribute("type").Value);
mapToList.Add(root.Attribute("mapTo").Value);
}
}
}
}
But during debugging I get NullReferenceException() in this line of code:
var registers = root.Element("unity").Element("container").Descendants("register");
As if .config doesn't have some of elements. I've checked .config file structure many times and I'm sure that it has to be like that, but still everything I've tried didn't worked. I was always ending up with null value for "unity".
P.S. I get a copy of .config file in root variable, so that's not a problem. Only as if elements of root don't exists or can't be found.
Anybody has some idea or how to fix this problem?
XElement root = XElement.Load(new XmlTextReader(@"C:\Users\nemanja.mosorinski\Downloads\__Research-master\__Research-master\SEDMSVSPackage\VisualStudioPackage\AppRes\ConfigFiles\Unity.config");
if(root != null)
{
XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
var registers = root
.Element(ns + "unity")
.Element(ns + "container")
.Descendants(ns + "register");
var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
}