我正在尝试编写一个代码,用于以这种格式从.config文件中获取元素:
<?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>
获取寄存器后,我想将寄存器类型和mapTo的值放在单独的列表中,使用以下代码:
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);
}
}
}
}
但是在调试过程中,我在这行代码中得到了NullReferenceException():
var registers = root.Element("unity").Element("container").Descendants("register");
好像.config没有一些元素。 我已经多次检查.config文件结构了,我确信它必须像那样,但是我尝试的所有内容都没有用。我总是以“统一”的空值结束。
P.S。我在根变量中获得了.config文件的副本,所以这不是问题。只是因为root的元素不存在或找不到。
任何人都有一些想法或如何解决这个问题?
答案 0 :(得分: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);
}