xml文件

<?xml version='1.0'?>
<!-- This file is a part of a book store inventory database -->
<bookstore xmlns="http://example.books.com"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://example.books.com Books.xsd">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

 

程序清单10-5 使用XmlReader处理Xml

Imports System.IO
Imports System.Xml

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim bookcount As Integer = 0

Using reader As XmlReader =
XmlReader.Create(Server.MapPath("books.xml"),
New XmlReaderSettings() With {
.IgnoreWhitespace = True,
.IgnoreComments = True})
While (reader.Read())
If (reader.NodeType = XmlNodeType.Element And "book" = reader.LocalName) Then
bookcount += 1
End If
End While
End Using

Response.Write(String.Format("Found {0} books!", bookcount))
End Sub

说明:

In Listing 10-5, the Books.xml file is in the same directory as this ASPX page, so a call to Server.MapPath gets the complete path to the XML file. The filename with full path is then passed into XmlReader.Create.

The Reader.LocalName property contains the non–namespace qualifi ed name of that node. The Reader.Name property is different and contains the fully qualifi ed name of that node, including namespace.

 

using xdocument rather Than xmlreader

Imports System.IO
Imports System.Xml
Imports System.Linq
Imports System.Xml.Linq
Imports <xmlns:b="http://example.books.com">

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim booksXML = XDocument.Load(Server.MapPath("books.xml"))
Dim books =
From book In booksXML...<b:book>
Select book.<b:title>.Value
Response.Write(String.Format("Found {0} books!", books.Count()))
End Sub
End Class

 

Notice the use of the Imports keyword to declare an XML namespace, as well as the use of “ . . . < > ” to indicate the method call to Descendants and “ . < > ” to call Elements .

 

Listing 10-12: Querying XML with XPathDocument and XPathNodeIterator

 As Listing 10-12 uses a read-only XPathDocument, it will not update the data in memory.

Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load document
Dim booksFile As String = Server.MapPath("books.xml")

Dim document As New XPathDocument(booksFile)
Dim nav As XPathNavigator = document.CreateNavigator()

'Add a namespace prefix that can be used in the XPath expression
Dim namespaceMgr As New XmlNamespaceManager(nav.NameTable)
namespaceMgr.AddNamespace("b", "http://example.books.com")

'All books whose price is not greater than 10.00
For Each node As XPathNavigator In nav.Select("//b:book[not(b:price[. > 10.00])]/b:price", namespaceMgr)
Dim price As Decimal = CType(node.ValueAs(GetType(Decimal)), Decimal)
Response.Write(String.Format("Price is {0}<BR/>", price))
Next

End Sub
End Class

If you then want to modify the underlying XML nodes, in the form of an XPathNavigator, you would use an XmlDocument instead of an XPathDocument. Your XPath expression evaluation may slow you down, but you will gain the capability to edit.

Listing 10-13: Querying and editing XML with XmlDocument and XPathNodeIterator

Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load document
Dim booksFile As String = Server.MapPath("books.xml")

Dim document As New XmlDocument()
document.Load(booksFile)

Dim nav As XPathNavigator = document.CreateNavigator()

'Add a namespace prefix that can be used in the XPath expression
Dim namespaceMgr As New XmlNamespaceManager(nav.NameTable)
namespaceMgr.AddNamespace("b", "http://example.books.com")

'All books whose price is not greater than 10.00
For Each node As XPathNavigator In nav.Select("//b:book[not(b:price[. > 10.00])]/b:price", namespaceMgr)
Dim price As Decimal = CType(node.ValueAs(GetType(Decimal)), Decimal)
node.SetTypedValue(price * CDec(1.2))
Response.Write(String.Format("Price raised from {0} to {1}<BR/>", price, CType(node.ValueAs(GetType(Decimal)), Decimal)))
Next
End Sub

End Class

Listing 10-13 changes the XPathDocument to an XmlDocument, and adds a call to XPathNavigator .SetTypedValue to update the price of the document in memory. The resulting document could then be persisted to storage as needed.

 

Listing 10-12q: Querying XDocuments with XPath Expressions

You can use XPath against an XDocument object by adding a reference to the System.Xml.XPath namespace. Adding this namespace reference to your code file’s scope adds new extension methods to the XDocument. You’ll get useful additions like CreateNavigator that gets you an XPathNavigator. You’ll also get the very useful XPathSelectElements. These extension methods are part of the “bridge classes” that provide smooth integration between System.Xml and System.Xml .Linq. Consider Listing 10-12q.

Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Linq

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load document
Dim booksFile As String = Server.MapPath("books.xml")
Dim document As XDocument = XDocument.Load(booksFile)

'Add a namespace prefix that can be used in the XPath expression
Dim namespaceMgr As New XmlNamespaceManager(New NameTable())
namespaceMgr.AddNamespace("b", "http://example.books.com")

'All books whose price is not greater than 10.00
Dim nodes = document.XPathSelectElements("//b:book[not(b:price[. > 10.00])]/b:price", namespaceMgr)

For Each node In nodes
Response.Write(node.Value + "<BR/>")
Next
End Sub

End Class