C# 从Initializationstring提取值并作为键值对返回
下面是应用程序初始化字符串的一部分:C# 从Initializationstring提取值并作为键值对返回,c#,.net,dictionary,initialization,C#,.net,Dictionary,Initialization,下面是应用程序初始化字符串的一部分: <Controls> <HtmlElement name="lnkLogIn" type="HtmlElement"> <AttributeMatchPath matchtype="equals"> <href>JavaScript:void(0)</href> <id>s_swepi_22</id> </AttributeM
<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>
JavaScript:void(0)
s_swepi_22
s_swepi_2
s_swepi_1
我想在代码背后做的是一个函数,它将每个控件的Inputelement名称和id作为键值对获取,并返回一个dictionary对象或类似的东西,我们可以从中提取键值对信息
这样做基本上是为了删除ID值的硬编码……因此,从init字符串中获取elementname和ID并将其存储为键值对的通用解决方案将非常棒……提前感谢:)
PS:使用C#…确定…完成…)
下面是我所做的:used 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 );
}
}
使用System.Linq;
使用System.Xml.Linq;
静态类程序
{
///
///应用程序的主要入口点。
///
公共静态void Main()
{
var xDoc=XElement.Load(“ApplicationInit.xml”);
var appSettingsDictionary=(xDoc.subjects(“InputElement”)
.选择(项目=>新建)
{
Key=item.Attribute(“name”).Value,
Value=item.subjections(“id”).First().Value
}
)
).ToDictionary(item=>item.Key,item=>item.Value);
}
}
}