簡體   English   中英

從Initializationstring中提取值並作為鍵值對返回

[英]Extracting values from Initializationstring and returning as a key-value pair

我下面有一個應用程序初始化字符串的一部分:

<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值的硬編碼來完成的。...因此,從init字符串中獲取elementname和id並將其存儲為鍵值對的通用解決方案確實很棒。...在此先感謝:)

PS:使用C#.....

OK完成.... :)

這是我所做的:使用過的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 );
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM