VB.NET读取XML和Skip行

时间:2014-07-19 04:26:43

标签: xml vb.net

NET和读取XML文件 我正在尝试构建跳过一些元素并直接访问我需要的其他元素的代码 这是因为我需要阅读很多信息。 因此,我直接访问我需要的信息

例如,我有xml文件..

<PSA_TEST>
      <STUDENT>
          <STUDENT1>
          </STUDENT1>
          <STUDENT2>
          </STUDENT2>
            ....100 students...
      </STUDENT>

      <TEACHER>
           <TEACHER1>
           </TEACHER1>
           <TEACHER2>
           </TEACHER2>
.......100 teacher
       </TEACHER>


</PSA_TEST>

我目前正在使用

Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

逐行读取所有测试信息。但是,我必须阅读超过1000行的xml文件。 因此,加载信息需要花费大量时间......

无论如何,直接访问student99或teacher99?

感谢

2 个答案:

答案 0 :(得分:0)

  • 试试这个

    Public Sub ReadXml()
      'strFileName = 'absolute/path/to/your/xmlfile.xml
      Dim XmlDoc As New XmlDocument
      If File.Exists(strFileName) Then
        XmlDoc.Load(strFileName)
        For Each child_Node As XmlNode In XmlDoc.DocumentElement.ChildNodes
            Select Case child_Node.Name
                Case "student99"
                    'to do
                Case "teacher99"
                    'to do
            End Select
        Next
      End If
    End Sub
    
  • 在这里你可以传递xmlfile和nodeName(ex.student99或teacher99)

    Public Sub ReadXml(ByVal strFileName As String, ByVal xmlNodeToRead As String)
      'strFileName = 'absolute/path/to/your/xmlfile.xml
      Dim XmlDoc As New XmlDocument
      If File.Exists(strFileName) Then
        XmlDoc.Load(strFileName)
        For Each child_Node As XmlNode In XmlDoc.DocumentElement.ChildNodes
            Select Case child_Node.Name
                Case "" & xmlNodeToRead & ""
                    'to do
            End Select
        Next
      End If
    End Sub
    

答案 1 :(得分:0)

考虑将XML重构为更加XML'。这里有一些代码可以激发你的想象力。

    Dim psatest As XElement = <PSA_TEST></PSA_TEST>
    Dim students As XElement = <students></students>
    Dim teachers As XElement = <teachers></teachers>

    'example - add some students
    For x As Integer = 1 To 5
        Dim student As XElement = <student sid=<%= x.ToString %>></student>
        student.Value = "I am student " & x.ToString
        students.Add(student)
    Next

    'add some teachers
    For x As Integer = 1 To 10
        Dim teacher As XElement = <teacher tid=<%= x.ToString %>></teacher>
        teacher.Value = "Teacher ID = " & x.ToString
        teachers.Add(teacher)
    Next

    'create psatest
    psatest.Add(students)
    psatest.Add(teachers)

    'show psatest
    Dim iterate As XNode = psatest

    Do
        Debug.WriteLine(iterate)
        iterate = iterate.NextNode
    Loop While Not IsNothing(iterate)

xml看起来像这样

<PSA_TEST>
  <students>
    <student sid="1">I am student 1</student>
    <student sid="2">I am student 2</student>
    <student sid="3">I am student 3</student>
    <student sid="4">I am student 4</student>
    <student sid="5">I am student 5</student>
  </students>
  <teachers>
    <teacher tid="1">Teacher ID = 1</teacher>
    <teacher tid="2">Teacher ID = 2</teacher>
    <teacher tid="3">Teacher ID = 3</teacher>
    <teacher tid="4">Teacher ID = 4</teacher>
    <teacher tid="5">Teacher ID = 5</teacher>
    <teacher tid="6">Teacher ID = 6</teacher>
    <teacher tid="7">Teacher ID = 7</teacher>
    <teacher tid="8">Teacher ID = 8</teacher>
    <teacher tid="9">Teacher ID = 9</teacher>
    <teacher tid="10">Teacher ID = 10</teacher>
  </teachers>
</PSA_TEST>

完成后,您可以像这样查询数据

    Dim sid As Integer = 3 ' student lower value

    'find one student
    Dim astudent As IEnumerable(Of XElement) = From s As XElement In psatest...<students>.Elements
                                               Where CInt(s.@sid) = sid Select s
    'show the results
    For Each std As XElement In astudent
        Debug.WriteLine(std.Value)
    Next

    Debug.WriteLine("")
    'find some students
    Dim sstudent As IEnumerable(Of XElement) = From s As XElement In psatest...<students>.Elements
                                               Where CInt(s.@sid) >= sid Select s
    'show the results
    For Each std As XElement In sstudent
        Debug.WriteLine(std.Value)
    Next