使用 C# 动态读取 XML 和执行函数 [英] Reading XML and executing functions dynamically using C#
本文介绍了使用 C# 动态读取 XML 和执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的xml:
I have a xml as below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<TestCase Name="1.7 Bus01">
<Action Word="Login" Text="Login" TestCaseId="MPD_QS_06">
<Parameter Name="userName" Value="user1"/>
<Parameter Name="userPwd" Value="user1"/>
<Parameter Name="Tenant" Value="tnt1"/>
<Parameter Name="expectedResult" Value="Login success"/>
</Action>
<Action Word="AddOrganisation" Text="AddOrganisation">
<Parameter Name="name" Value="MAHNEWX"/>
<Parameter Name="senderId" Value=""/>
<Parameter Name="address" Value="77 Westferry Circus"/>
<Parameter Name="postCode" Value="E77 4 HB"/>
<Parameter Name="city" Value="London"/>
<Parameter Name="country" Value="United Kingdom"/>
<Parameter Name="state" Value=""/>
<Parameter Name="email" Value=""/>
<Parameter Name="telCountryCode" Value=""/>
<Parameter Name="telNumber" Value=""/>
<Parameter Name="telExtension" Value=""/>
<Parameter Name="faxCountryCode" Value=""/>
<Parameter Name="faxNumber" Value=""/>
<Parameter Name="faxExtension" Value=""/>
<Parameter Name="roleList" Value="MAH"/>
<Parameter Name="expectedResult" Value="Organisation is added"/>
</Action>
<Action Word="NewValidationRegistration" Text="NewValidationRegistration">
<Parameter Name="registrationname" Value="FusionUnion"/>
<Parameter Name="AS2Identifier" Value="MPTVC"/>
<Parameter Name="expectedResult" Value="Registration is validated"/>
</Action>
<Action Word="NewSubmissionRegistration" Text="NewSubmissionRegistration">
<Parameter Name="registrationname" Value="FusionUnion"/>
<Parameter Name="AS2Identifier" Value="MPTVC"/>
<Parameter Name="expectedResult" Value="Registration is submitted"/>
</Action>
<Action Word="Logout" Text="Logout">
<Parameter Name="expectedResult" Value="Logout success"/>
</Action>
</TestCase>
对于每个动作词,我都有一个方法.如何读取此 xml 并使用参数动态执行方法.Parameter Name 是我的方法中的实际变量.每次参数的数量都不同.准确地说,我想读取 xml 并获取动作词列表,然后使用这些值执行它们.提前感谢您的帮助.
For each Action Word I have a method. How can I read this xml and execute the methods dynamically with parameters. The Parameter Name s are actual variables in my methods. The number of parameters differs each time. Precisely, I want to read the xml and get the list of action words and then execute them using the values. Thanks for ur help in advance.
-萨西
推荐答案
object obj = this; //your object containing methods
XDocument xDoc = XDocument.Parse(xml);
Type type = obj.GetType();
foreach (var action in xDoc.Descendants("Action"))
{
MethodInfo mi = type.GetMethod(action.Attribute("Word").Value);
var dict = action.Descendants().ToDictionary(
d=>d.Attribute("Name").Value,
d=>d.Attribute("Value").Value);
object[] parameters = mi.GetParameters()
.Select(p => Convert.ChangeType(dict[p.Name],p.ParameterType))
.ToArray();
var expectedResult = mi.Invoke(obj, parameters);
Debug.Assert(expectedResult.Equals(dict["expectedResult"]));
}
这篇关于使用 C# 动态读取 XML 和执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文