我想寫需要從團結config文件的一些元素的方法。每個元素都有類型和mapTo屬性,並且我將它們的字符串值,得到我需要的子字符串並將它們放在兩個分隔列表中。之後,我想在一些數據網格中編寫列表的內容。的foreach工作不正常
的問題是,一精加工畢竟foreach循環代碼返回到一個用於檢查單個寄存器,並在另兩個再次進入用於類型和mapTo值。 換句話說,在列表中,我獲得了無數的字符串值,而不是隻獲取一次。 我是一個初學者,我嘗試了很多東西,但沒有任何接縫可以完成工作。 任何人有一個想法,我得到錯誤? 方法在C#中的代碼看起來是這樣的:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open XML file";
fDialog.Filter = "XML files|*.config";
fDialog.InitialDirectory = @"C:\";
bool? control = fDialog.ShowDialog();
if (control.Value)
{
var filePath = fDialog.FileName;
ReadAdvancedConfigFile(filePath);
}
}
而且在:
private void ReadAdvancedConfigFile(string path)
{
XElement root = null;
root = XElement.Load(new XmlTextReader(path));
if (root != null)
{
XNamespace ns = "http://schemas.microsoft.com/practices/2010/unity";
var registers = root.Element(ns + "unity").Element(ns + "container").Descendants(ns + "register");
if (registers.Count() > 0)
{
var tipList = registers.Select(x => x.Attribute("type").Value);
var mapToList = registers.Select(x => x.Attribute("mapTo").Value);
List<string> listresult = new List<string>();
List<string> listresultm = new List<string>();
foreach (var reg in registers)
{
foreach (var tpl in tipList)
{
var end = tpl.IndexOf(',');
var start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1;
var result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start);
listresult.Add(result);
}
foreach (var mpl in mapToList)
{
var endm = mpl.IndexOf(',');
var startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1;
var resultm = mpl.Substring(startm, (endm == -1 ? mpl.Length : endm) - startm);
listresultm.Add(resultm);
}
int maxLenList = Math.Max(listresult.Count, listresultm.Count);
for (int i = 0; i < maxLenList; i++)
{
if (i < listresult.Count && i < listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresult[i], listresultm[i]));
}
else if (i >= listresult.Count)
{
_obsCollection.Add(new Tuple<string, string>(string.Empty, listresultm[i]));
}
else if (i >= listresultm.Count)
{
_obsCollection.Add(new Tuple<string, string>(listresultm[i], string.Empty));
}
}
}
tabela.ItemsSource = _obsCollection;
}
}
}
方法從一個按鈕和一個名爲負載從一些位置找到Unity.config文件在文件系統中像這樣獲得通話Unity.config是這種格式(我刪除大部分元素,以相同的一些空間在這裏,它會與這個工作太)的XML文件:
<?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">
<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>
<!--<property name="LocalisationService" dependencyName="LocalisationService" />-->
<!--This is a property injection from the language plugin -->
</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>
我們無法找到的bug在你的代碼當我們甚至不能運行它。請學會調試代碼:http://www.youtube.com/watch?v=C0vDKXIq_9A – Euphoric
作爲欣快說:縮小你的代碼。創建一個演示項目,刪除所有無助於解決問題的代碼並將其發佈到此處。 –
我正在做一個調試,但仍然不知道什麼是錯的。不管怎樣,謝謝......我會繼續努力:) –