XML Element Error Object reference not set to an instance of an object

Hi!
After Deserialize XML in XDoc (type XDocument) I do For Each on XDoc.Elements(“MyNode1”).Elements(“MyNode2”).Elements(“MyNode3”)
But no MyNode3 element in xml-file and I get error “Object reference not set to an instance of an object” in For Each.

I parsing different xml-files and I don’t know if this element is in the file or not.
How to check there is an element in XDoc or not?

@Aleksey_M
a rudimentary quickcheck would be

  • run on debug
  • set a breakpoint after deserialize the document
    use trhe immediate / watchpanel and check with:
  • YourXDocVar.Root.Descendants(ChildrenPositionIndexAsInt).Value.toString
  • YourXDocVar.Root.Descendants(ChildrenPositionIndexAsInt).Name.toString

ChildrenPositionIndexAsInt e.g. 1 for the first child,2 for second…

maybe your XML is defining namespaces. The best would be you share the XML or similar sample data with us

In the directory, where I am parsing the xml, xml-files can be of different formats. This is normal.
And if the robot took a different format for analysis, where there are no my MyNode1,2,3 etc elements, it did not break when called XDoc.Elements(“MyNode1”).Elements(“MyNode2”).Elements(“MyNode3”)