In the application I am working on, there is an XML document that needs to be loaded into memory for querying. To do this, I am calling XmlDocument.Load(string path), which has been working fine. However, I recently came across some weird behavior with the method.
I have a generated file that looks like this (edited to remove sensitive data):
*Snip*
<Info name="name2" parent="name1" modifier="A"/>
<Info name="name2" parent="name1" modifier="B"/>
<Info name="name1" modifier="A"/>
<Info name="name1" modifier="B"/>
<Info name="name3" parent="name2" modifier="A"/>
<Info name="name3" parent="name2" modifier="B"/>
*Snip*
Above there are six entries. However, when calling XmlDocument.Load, only 4 entries are loaded, leaving the first 2 entries out. I’ve confirmed this with breakpoints and walking through the data debugger.
However, when I manually rearrange the data in the document to be
*Snip*
<Info name="name1" modifier="A"/>
<Info name="name1" modifier="B"/>
<Info name="name2" parent="name1" modifier="A"/>
<Info name="name2" parent="name1" modifier="B"/>
<Info name="name3" parent="name2" modifier="A"/>
<Info name="name3" parent="name2" modifier="B"/>
*Snip*
all the data is loaded correctly, none of the elements are left out.
As far as I can tell, the document is well formed, and there are no exceptions thrown when loading it. It should also be noted that when I artificially add a parent attribute to all the entries, they all load correctly, but if I understand XML correctly, that shouldn’t matter and it will just nullify missing attributes in code. Also, there are no namespaces specified for any of the elements.
Is this expected behaviour, or am I missing something?
I can give more details if needed.
I just tried your example as such:
And it worked fine. It returned a value of 6, which is the correct number. Try it for yourself. If it doesn’t work the same for you, there must be something screwy with your installation. If, however, as I suspect, it does work the same for you, then the problem must be something else.
Try starting small and adding more complexity until it fails. Either there’s something else wrong in the XML that’s causing it, or there is a bug in the way you’re reading the nodes from the XmlDocument object.