I have this very simple peace of code and a simple XML file . I am reading each node and writing it to another file , And really strage that xml reader skips every alternate record node . It writes 1 and 3 rd node from following file ! Any help appreciated .
Do While (reader.Read())
If (reader.NodeType = XmlNodeType.Element And (reader.LocalName = "record" Or reader.LocalName = "record1")) Then
writer.WriteNode(reader, True)
writer.Flush()
End If
Loop
–
<?xml version="1.0" encoding="UTF-8"?>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="world-check.xsd">
<record>
<foo>
<bar>wtf3</bar>
<bar>wtf4</bar>
</foo>
</record>
<record>
<foo>
<bar>wtf4</bar>
<bar>wtf5</bar>
</foo>
</record>
</records>
Forgive my VB, I’m pretty much purely a C# developer.
XmlWriter.WriteNode() does an XmlReader.Read() past the EndElement node for the node you write, so when you get back to the start of the While loop you read past the next Record node.
Try this: