2012-04-13 132 views
1

我需要從xml文件中選擇多個元素的幫助。我目前只知道如何選擇一個,並試圖找出不止一個傷害我的小鬼科普里。謝謝你的時間。從xml文件中選擇多個元素,並顯示多個標籤

Imports System.Xml 

Partial Class ex01_Default 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     'instantiate a reader' 
     Dim reader As New XmlTextReader(Server.MapPath("~/ex01/docP.xml")) 
     'declare variable to record when a <name> element is found' 
     Dim bName As Boolean = False 
     'iterate through all of the nodes in the XML document' 
     While reader.Read() 
      'look at the node type of each node' 
      Select Case reader.NodeType 
       'if node is a <name> element, remember it' 
       Case XmlNodeType.Element 
        If reader.Name = "title" Then 
         bName = True 
        End If 


        'if node is text & previous node was <name>, add it to Label' 
       Case XmlNodeType.Text 
        If bName Then 
         lblDisplayXml.Text &= reader.ReadString & "<br/>" 
         bName = False 

        End If 
      End Select 
     End While 
    End Sub 
End Class 

XML文件:

<book_club> 
    <book> 
    <isbn>0-13-129014-2</isbn> 
    <title>JAVA How to Program (6th Ed)</title> 
    <author>PJ &amp; HM Deitel</author> 
    <price>£39.99</price> 
    </book> 
    <book> 
    <isbn>0-67-232238-2</isbn> 
    <title>Teach Yourself UML</title> 
    <author>J Schmuller</author> 
    <price>£9.99</price> 
    </book> 
    <book> 
    <isbn>0-27-365575-2</isbn> 
    <title>Practical Business Systems Development using SSADM</title> 
    <author>P Weaver, N Lambrou &amp; M Walkley</author> 
    <price>£34.99</price> 
    </book> 
    <book> 
    <isbn>0-67-232422-9</isbn> 
    <title>XML Primer Plus</title> 
    <author>N Chase</author> 
    <price>£32.99</price> 
    </book> 
    <book> 
    <isbn>0-78-972476-6</isbn> 
    <title>XML and Java from Scratch</title> 
    <author>N Chase</author> 
    <price>£19.99</price> 
    </book> 
    <book> 
    <isbn>1234567890</isbn> 
    <title>ASP.NET for Dummies</title> 
    <author>RUA Dummy</author> 
    <price>free!!</price> 
    </book> 
</book_club> 
+1

你可以使用.net> = 3.5如果是的話你可以試試linq – 2012-04-13 22:53:20

回答

2

我不知道很多關於VB,因爲我使用C#。我使用LinQ,因爲它使用XDocument類提供了一種更新的方式來讀取XML。這是我做的事,我從一個字符串讀取:

方法返回的IEnumerable()

XDocument doc = XDocument.Parse(myString); 
      var query= from x in doc.Descendants("Book") 
          select new Location 
          { 
           ISBN= Double.Parse(
            x.Descendants("isbn").First().Value), 
           Author= Double.Parse(
            x.Descendants("author").First().Value) ...... 
          }; 

      return query; 

創建的書類

公共類圖書 { 公共圖書(){}

public string Author { 
    get; set; 
} 
..... 

}