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?

nemo_87
  • 4,523
  • 16
  • 56
  • 102

1 Answers1

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);
}
xinux
  • 967
  • 8
  • 14
  • 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 elements in between. – nemo_87 Feb 03 '14 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 03 '14 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 03 '14 at 10:22
  • @nemo_87 I think I managed to create a file that reproduces your problem. – xinux Feb 03 '14 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 elements. – nemo_87 Feb 03 '14 at 10:38
  • @PeterSmith Still throwing the same NullReferenceException() on that line of code. :( – nemo_87 Feb 03 '14 at 12:14
  • @PeterSmith Now I checked registers variable in debugger an there is a list of registers in it...but even thou registers is not empty, something is still wrong. New NullReferenceException now happened in this line: tipList.Add(root.Attribute("type").Value); – nemo_87 Feb 03 '14 at 12:33
  • @nemo_87 Oh, it did? The problem with Count is solved? Is it really root you want to get the attribute from? Who is PeterSmith? – xinux Feb 03 '14 at 12:38
  • For example I want to get this string from type: "Web.WebClient.Services.IScaleCoefConfigService, Web.WebClient.TDMSWebApp" After that I will need to get substring from it that I will use. That's why I want to access to those attributes. I've put new definitions for tipList and mapToList and it's working now. Only I had to delete: List tipList = new List(); List mapToList = new List(); Cause it didnt want to work with that, of course. P.S. I am stupid, I thought that @PeterSmith is your username, and thats only a example of how to tag someone in comment :P – nemo_87 Feb 03 '14 at 12:49