XmlTextReader读取特定节点的每秒大量

时间:2015-08-24 12:20:53

标签: xml vb.net xmltextreader

下面找到我正在使用的xml示例和代码。当我与WhiteSpacehandling = None一起使用时,从4本书记录中我只收到2本书(每两本书)。解决方案我发现将WhiteSpaceHandling设置为WhiteSpacehandling = All - 然后我得到4条记录,因为在xml中每行的末尾放置CR(回车)因此读者正在以某种方式使用它来正确地从xml获取所有数据当参数设置为WhiteSpacehandling = All时。不幸的是,当XML文件数据全部在一行中时仍然存在问题,那么即使设置WhitespaceHandling to All也无济于事,我会检索每一秒的书籍数据(因为没有CR)。你知道我怎么能处理这个问题?即使数据放在一行中,如何始终获取所有数据?

XML文件:

 <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 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

图片1:

enter image description here

图片2:

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个

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
​