My code is:
XmlDoc := NewXMLDocument;
with XmlDoc do
Options := Options + [doNodeAutoIndent];
[...]
N := Parent.ChildNodes.FindNode('KONTRAHENT');
while N <> nil do
begin
if N.ChildNodes['ID_KONTRAHENTA'].NodeValue = KontrNr then
DoSomething;
N := N.NextSibling;
end;
and XML like this:
<KARTOTEKA_KONTRAHENTOW>
<KONTRAHENT>
<ID_KONTRAHENTA>925</ID_KONTRAHENTA>
</KONTRAHENT>
<KONTRAHENT>
<ID_KONTRAHENTA>1208</ID_KONTRAHENTA>
</KONTRAHENT>
</KARTOTEKA_KONTRAHENTOW>
data viewed in Watch Parent.XML looking that:
'<KARTOTEKA_KONTRAHENTOW>'#$D#$A#9#9'<KONTRAHENT>'#$D#$A#9#9#9'<ID_KONTRAHENTA>925</ID_KONTRAHENTA>'#$D#$A#9#9'</KONTRAHENT>'#$D#$A#9#9'<KONTRAHENT>'#$D#$A#9#9#9'<ID_KONTRAHENTA>1208</ID_KONTRAHENTA>'#$D#$A#9#9'</KONTRAHENT>'#$D#$A#9'</KARTOTEKA_KONTRAHENTOW>'
And when i read nodes in loop, when Options: [doNodeAutoIndent] is set then i have some nodes like this: N = '#$D#$A#9#9' and Count of Nodes = 7 (instead 2, in this example)
Without doNodeAutoIndent, all is OK and Count of Nodes = 2, but my XML file is in one line :(
The question is: How to bypass empty NextSibling when doNodeAutoIndent enabled?
What you are describing is perfectly normal. Whitespace between elements is treated as additional text nodes within the hierarchy, eg for your example XML, its DOM tree looks like the following:
That is why the node count is higher than you are expecting.
You need to account for these extra text nodes while looping though the elements, eg:
Alternatively, use an XPath query instead to focus on just the nodes you really want, eg: