2012-07-11 56 views
0

下面我有一个应用程序初始化字符串的一部分:提取从Initializationstring值,并返回一个键值对

<Controls> 
    <HtmlElement name="lnkLogIn" type="HtmlElement"> 
    <AttributeMatchPath matchtype="equals"> 
     <href>JavaScript:void(0)</href> 
     <id>s_swepi_22</id> 
    </AttributeMatchPath> 
    </HtmlElement> 
    <InputElement name="tbPassword" type="InputElement"> 
    <AttributeMatchPath matchtype="equals"> 
     <id>s_swepi_2</id> 
    </AttributeMatchPath> 
    </InputElement> 
    <InputElement name="tbUserID" type="InputElement"> 
    <AttributeMatchPath matchtype="equals"> 
     <id>s_swepi_1</id> 
    </AttributeMatchPath> 
    </InputElement> 
</Controls> 

什么,我想在后面的代码做的是得到Inputelement名称和功能每个控件的id作为键值对,并返回一个字典对象或类似的东西,我们可以从中提取键值对信息。

这基本上是为了去除ID值的硬编码....所以一个通用的解决方案,从初始化字符串中获取元素名称和ID并将它们存储为键值对将非常好....感谢提前:)

PS:使用C#.....

回答

0

好吧...完成.... :)

这里就是我所做的:使用System.Xml.Linq的功能:

以下是工作代码:

using System.Linq; 
using System.Xml.Linq; 

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    public static void Main () 
    { 
     var xDoc = XElement.Load ("ApplicationInit.xml"); 
     var appSettingsDictionary = (xDoc.Descendants("InputElement") 
      .Select (item => new 
          { 
           Key = item.Attribute("name").Value, 
           Value = item.Descendants("id").First().Value 
          } 
        ) 
       ).ToDictionary (item => item.Key, item => item.Value); 
    } 
} 

}