xmlvb.netxmltextreader

XmlTextReader reads every second bulk of specific node


Below find xml sample and code i am using. When i was using with WhiteSpacehandling = None then out from 4 books records i was receiving only 2 (every second book). Solution i found was to use WhiteSpaceHandling set to WhiteSpacehandling = All - then i get 4 records because at the end of each line in xml is placed CR (carriage return) therefore reader is using it somehow to correctly take all the data from xml when parameter set to WhiteSpacehandling = All. Unfortunetly there is still problem when XML file data would be all in one line then even setting up WhitespaceHandling to All will not help and i would retreive every second book data (because no CR). Do you know how can i handle that problem? How to take always all data even if data is placed in one line?

XML file:

 <bookstore>
     <book genre="autobiography">
       <title>Potop</title>
       <author>
         <first-name>Benjamin</first-name>
         <last-name>Franklin</last-name>
       </author>
       <price>8.99</price>
     </book>
     <book genre="novel">
       <title>Faraon</title>
       <author>
         <first-name>Herman</first-name>
         <last-name>Melville</last-name>
       </author>
       <price>11.99</price>
     </book>
     <book genre="philosophy">
       <title>Ben Hur</title>
       <author>
         <name>Plato</name>
       </author>
       <price>9.99</price>
     </book>
     <book genre="scifi">
       <title>Terminator1</title>
       <author>
         <name>Plato</name>
       </author>
       <price>9.99</price>
     </book>
   </bookstore>

Code i am using:

 'Create the XML Reader
        Dim reader = New XmlTextReader("books.xml")

        reader.WhitespaceHandling = WhitespaceHandling.All  'WhitespaceHandling.None   '<----------


        Do While (reader.Read())
             If reader.IsStartElement And reader.NodeType = XmlNodeType.Element And reader.Name = "book" Then
            Console.WriteLine(reader.ReadOuterXml)
        End If
            End If

        Loop

Picture 1:

enter image description here

Picture 2:

enter image description here


Solution

  • Try this

    Imports System.IO
    Imports System.Xml
    Module Module1
    
        Sub Main()
            Dim input As String = _
            "<bookstore>" & _
         "<book genre=""autobiography"">" & _
           "<title>Potop</title>" & _
           "<author>" & _
             "<first-name>Benjamin</first-name>" & _
             "<last-name>Franklin</last-name>" & _
           "</author>" & _
           "<price>8.99</price>" & _
         "</book>" & _
         "<book genre=""novel"">" & _
           "<title>Faraon</title>" & _
           "<author>" & _
             "<first-name>Herman</first-name>" & _
             "<last-name>Melville</last-name>" & _
           "</author>" & _
           "<price>11.99</price>" & _
         "</book>" & _
         "<book genre=""philosophy"">" & _
           "<title>Ben Hur</title>" & _
           "<author>" & _
             "<name>Plato</name>" & _
           "</author>" & _
           "<price>9.99</price>" & _
         "</book>" & _
         "<book genre=""scifi"">" & _
           "<title>Terminator1</title>" & _
           "<author>" & _
             "<name>Plato</name>" & _
           "</author>" & _
           "<price>9.99</price>" & _
         "</book>" & _
       "</bookstore>"
    
            'Create the XML Reader
            Dim sReaader As New StringReader(input)
            Dim reader = New XmlTextReader(sReaader)
    
    
            reader.ReadToFollowing("book")
            Do While Not reader.EOF
                Dim line As String = reader.ReadOuterXml
                Console.WriteLine(line)
    
            Loop
    
        End Sub
    
    End Module
    ​