【问题标题】:Extracting values from Initializationstring and returning as a key-value pair从 Initializationstring 中提取值并作为键值对返回
【发布时间】:2012-07-11 11:52:06
【问题描述】:

我在下面有一部分应用程序初始化字符串:

<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 name 和 id 作为键值对,并返回一个字典对象或类似的东西,我们可以从中提取键值对信息。

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

PS:使用 C#.....

【问题讨论】:

    标签: c# .net dictionary initialization


    【解决方案1】:

    好的...完成.... :)

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

    }

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多