The AIOWEB_tPriceBand
elements are not located directly under the root
element. Instead, they are actually located under the /root/dataroot
element.
The reason that the first code sample works is because it is reading the xmlDoc.Root.Elements.<AIOWEB_tPriceBand>
elements. You'll notice that it is not accessing the AIOWEB_tPriceBand
elements directly under Root
. Instead, it's accessing them from Root.Elements
, which is actually the collection of all of the child elements of the Root
element. Since dataroot
is a child of the Root
element, it will look for them under there. To make the first code sample mimic the second one, you would have needed to write it this way:
' Won't work because it looks for /root/AIOWEB_tPriceBand
myXMLRowCount = xmlDoc.Root.<AIOWEB_tPriceBand>.Count()
The less confusing way to have written it, to make it work properly, would have been like this:
' Will work because it looks for /root/dataroot/AIOWEB_tPriceBand
myXMLRowCount = xmlDoc.Root.<dataroot>.<AIOWEB_tPriceBand>.Count()
Now that it's more clear why it was that the first example was actually working, now it should be more clear how to get the second example to work:
myXMLRowCount = xmlDoc.Root.<dataroot>.Elements(myXmlElement).Count()
Alternatively, if you don't care where, exactly, in the document structure, that the AIOWEB_tPriceBand
elements actually exist, and all you want to do is get a total count of them, where ever in the document they happen to be, then you can simply search for any descendants of the root by that name, rather than specifying the path down through the dataroot
element:
myXMLRowCount = xmlDoc.Root...<AIOWEB_tPriceBand>.Count()
Or, by string:
myXMLRowCount = xmlDoc.Root.Descendants(myXmlElement).Count()
I should also mention that you really ought to be using MessageBox.Show
rather than the old VB6-style MsgBox
method.