2014-02-05 85 views
1

我想写需要从团结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> 
+2

我们无法找到的bug在你的代码当我们甚至不能运行它。请学会调试代码:http://www.youtube.com/watch?v=C0vDKXIq_9A – Euphoric

+0

作为欣快说:缩小你的代码。创建一个演示项目,删除所有无助于解决问题的代码并将其发布到此处。 –

+0

我正在做一个调试,但仍然不知道什么是错的。不管怎样,谢谢......我会继续努力:) –

回答

1

我调整你的代码,现在它的工作原理(删除foreach):

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 (string tpl in tipList) 
     { 
      int end = tpl.IndexOf(','); 
      int start = tpl.LastIndexOf('.', (end == -1 ? tpl.Length - 1 : end)) + 1; 
      string result = tpl.Substring(start, (end == -1 ? tpl.Length : end) - start); 
      listresult.Add(result); 
     } 

     foreach (string mpl in mapToList) 
     { 
      int endm = mpl.IndexOf(','); 
      int startm = mpl.LastIndexOf('.', (endm == -1 ? mpl.Length - 1 : endm)) + 1; 
      string 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)); 
      } 
     } 
    } 
} 
+0

是啊,这完全工作,谢谢:) 所以我有一个问题。你几乎把所有的var改为string和int类型。有一些与此有关吗?像是有问题,如果我们使用太多的VAR定义或类似的东西? P.S.我现在有一个新问题,而不是从datagrid中的列表中获取这些字符串,我得到了一些行。但是,这是全新的问题:) –

+0

对不起,我改变了'var'变量只是为了让自己清楚发生了什么。我个人不喜欢'var'。 –

+0

@ nemo_87:在一个新问题中发表一个例子,我会看一看。 –

2

在我看来,如果你不需要对于每个循环。你已经获取的所有“类型”和“mapTo”属性,当你查询使用LINQ:

var tipList = registers.Select(x => x.Attribute("type").Value); 
var mapToList = registers.Select(x => x.Attribute("mapTo").Value); 

这有效地为您提供了“寄存器”的中xelements的所有属性。 你甚至没有使用你的循环内的VAR“章” ......

+0

感谢您的回应。我会努力的时候了,让你知道做它的工作:) –

+0

它确实当您删除'的foreach(VAR REG寄存器)工作' –

+0

@Reinder试图删除的foreach(在寄存器VAR REG),但问题仍然存在。我完全搞砸了一些真正糟糕的事情。 : -/ 但你说得对,那个循环没有做任何事情,这是完全没有必要的。退出两个foreach循环后,它返回到var tipList和var mapToList,并一次又一次地将值复制到列表中。 –