0

I have IEnumerable<XElement> theTestCaseNodes which has the following kind of XElements

<testcase>
    <Main>  
       <test_step type ="action">
           <Name>Goto</Name>
           <description>xxxxxxxxx</description>    
       </test_step>  
       <test_step type ="check">
           <Name>Click</Name>
           <description>xxxxxxxxx</description>
       </test_step>  
    </Main>  
</testcase>
<testcase>
    <Main>  
       <test_step type ="action">
           <Name>Goto</Name>
           <description>xxxxxxxxx</description>    
       </test_step>  
       <test_step type ="check">
           <Name>Type</Name>
           <description>xxxxxxxxx</description>
       </test_step>  
    </Main>  
</testcase>

Basically this is my testcase and I want to execute them in an order. So, now I want to read each node in IEnumerable using XMLReader.

Please help how to proceed!!

I understood that i need to use "using" but not sure how to proceed.

public void ExecuteTestcase(IEnumerable<XElement> theTestCaseNodes)
{
    using (XmlReader aNodeReader = XmlReader.ReadSubtree()) {

    }
}
Ignas
  • 4,092
  • 17
  • 28
Diana
  • 49
  • 1
  • 7
  • 1
    You could write your xml to a string, create a `StringReader` on that and pass it to the `XmlReader`. Note you have to `Create` a reader in the `using` then you can call instance methods like `ReadSubtree`. – juharr Jul 25 '17 at 12:25
  • _Why_ do you want to use XmlReader? That is used to convert text to DOM, but you already have DOM level elements. – H H Jul 25 '17 at 15:41
  • @HenkHolterman, Please let me know more about this. I'am new to XML.I want to use XMLReader here because I want to go in order. There are 2types of steps, "type=Action" and "type=check" and I want to read them in order. – Diana Jul 26 '17 at 06:38
  • 1
    The whole idea of order is a bit tricky in XML but `xElement.Elements("test_step")` should give you what you need. – H H Jul 26 '17 at 07:06

1 Answers1

0

Use XElement.CreateReader for instantiating XmlReader for the element.

public void ExecuteTestcase(IEnumerable<XElement> theTestCaseNodes)
{
    foreach(var node in theTestCaseNodes)
    {
        using (var reader = node.CreateReader())
        {
            // use XmlReader for testing
        }
    }
}

XNode.CreateReader Method ()

If main purpose of XmlReader is reading elements in correct order then looping through elements will execute them in same order they appear in xml

foreach(var testStep in theTestCaseNodes.Elements("test_step"))
{
    // execute step
}

If you don't want rely on order of your xml then you can access correct step by type attribute

foreach(var testStep in theTestCaseNodes.Elements("Main"))
{
    var action = testStep.Elements("test_step")
                         .First(step => step.Attribute("type") == "action");
    var check = testStep.Elements("test_step")
                        .First(step => step.Attribute("type") == "action");

    // execute action
    // execute check
}
Fabio
  • 31,528
  • 4
  • 33
  • 72