0

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?

1 Answer 1

0
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);
}
9
  • No, no they are not actually there. The real XML file was too big and too messy for forum, so I wrote a short version, just to show the structure and hierarchy of it. Those dots in example are suppose to show that there is lots of <register> elements in between.
    – nemo_87
    Feb 3, 2014 at 10:12
  • Your example config is parsed without problems if you remove the dots and commas. There must be some difference in your 'real' file causing the problem. Without seeing it it's hard to help.
    – xinux
    Feb 3, 2014 at 10:17
  • The real XML file gets parsed, I've check that during the debugging in root variable I get everything from the original file. Problem is somewhere else. I will put original xml file, but unfortunately I will have to do it later, cause I'm still new member and I don't have all privileges on the forum.
    – nemo_87
    Feb 3, 2014 at 10:22
  • @nemo_87 I think I managed to create a file that reproduces your problem.
    – xinux
    Feb 3, 2014 at 10:33
  • I was able to edit my first post, so that's how the real xml looks like. Only it has lot more <register> elements.
    – nemo_87
    Feb 3, 2014 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.