1

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> 

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.

-Sasi

2
  • you can use Invoke() msdn.microsoft.com/en-us/library/a89hcwhh.aspx Commented Jul 17, 2012 at 20:26
  • If you want to keep it simple, you can go throught the XML using XmlDocument, XmlNode and XmlAttribute classes. To invoke the right method, just switch by the name of the method. I suppose you could use more elegant way to do it, but this seems to be the most straightforward and simple way to do it. It also allows you to keep actual methods named differently than the XML Action names. Commented Jul 17, 2012 at 20:27

2 Answers 2

3
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"]));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all for the answers. This helped me a lot. I was always trying to have a dictionary with string and action which made me hard to find a way to handle the parameters.
1

I've done something broadly similar for a project I'm working on. From a high level perspective, if you can assume that the Action Word is exactly the name of a method in some assembly, then you can use reflection to get a MethodInfo corresponding to the actual function. You can then invoke the function, passing in the appropriate parameters to the method.

The one catch here is how to specify the parameters. Since there will be a variable number of parameters, you need to use a data structure which is capable of handling the variable list. I'd suggest using a Dictionary to pass the parameters.

Ok, so assuming that you can identify and load the appropriate assembly, proceed something like this:

foreach Action:

  • retrieve the value of the Word attribute into actionWordString
  • create a new Dictionary instance
  • foreach Parameter in Action:
  • retrieve the Name and Value attribute values
  • add a new entry in your Dictionary: i.e. dict[nameString] = valueString
  • use reflection to find the MethodInfo with the same name as the actionWordString and which also takes a Dictionary as a parameter
  • invoke the Method, passing in the Dictionary you previously created and populated.

It takes as much prose to describe as it does code to implement. One of the harder things, at least as I find it, is to load or access the Type of the Assembly which contains the methods. Also, it would probably be best to implement the methods as static, so that you don't have to be concerned about creating an instance of the handler class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.