3

I have a xml file of the form :

<Level1>
   <Level2>
        <Level3>
             <Level4 attr1 = "123.4" attr2 = ""> </Level4>
        </Level3>
   </Level2>
<Level1> 

I'm using XUnit to check the structure of the xml.

[Fact]
public void Xml_Check()
{
    var doc = XDocument.Load("test.xml");
    doc.Should().HaveRoot("Level1");
    doc.Should().HaveElement("Level2");
    doc.Should().HaveElement("Level3");  //Erroring on this line
}

I'm getting the error: Expected XML document <Level1>...</Level1> to have root element with child "Level3" but no such child element was found. It is trying to treat Level3 as a child of Level1 instead of Level2.

How do I get the Level3 and check whether certain attributes exist in Level4? Is there a way to check the type of the attribute value?

Dev
  • 1,451
  • 20
  • 30
  • 2
    Level1,2,3 are all under root, you should select element level2, then apply `level2.Should().HaveElelment("Level3")` – iSR5 Oct 11 '19 at 12:36
  • @iSR5 I tried level2.Should().HaveElement("Level3") - that throws an error level2 doesn't exist in the current context – Dev Oct 11 '19 at 12:49

1 Answers1

5

Finally got it working and someone might find it useful in future

It should be:

doc.Should().HaveElement("Level2").Which.Should().HaveElement("Level3");
Dev
  • 1,451
  • 20
  • 30