|
|
| .NET Programming |
How To Read XML Data from a URL by Using Visual Basic .NET |
Imports System.Xml
Module Module1
Sub Main()
Const URLString As String = "http://localhost/books.xml"
Dim reader As XmlTextReader = New XmlTextReader(URLString)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Console.Write("<" + reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Console.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.Write("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
End Sub
End Module
|
How To Read XML from a File by Using Visual Basic .NET |
|
Imports System.Xml Module Module1 Sub Main() Dim reader As XmlTextReader = New XmlTextReader("books.xml") Do While (reader.Read()) Select Case reader.NodeType Case XmlNodeType.Element 'Display beginning of element. Console.Write("<" + reader.Name) If reader.HasAttributes Then 'If attributes exist While reader.MoveToNextAttribute() 'Display attribute name and value. Console.Write(" {0}='{1}'", reader.Name, reader.Value) End While End If Console.WriteLine(">") Case XmlNodeType.Text 'Display the text in each element. Console.WriteLine(reader.Value) Case XmlNodeType.EndElement 'Display end of element. Console.Write("</" + reader.Name) Console.WriteLine(">") End Select Loop Console.ReadLine() End Sub End Module
|
How To Read XML Data from a Stream in .NET Framework SDK |
Imports System.Xml
Imports System.IO
Module Module1
Sub Main()
Dim stream As System.IO.StreamReader
' Loads the XML data in the file books.xml in a new stream.
stream = New StreamReader("books.xml")
Dim reader As XmlTextReader = New XmlTextReader(stream)
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.
Console.Write("<" + reader.Name)
If reader.HasAttributes Then 'If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Console.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.Write("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
End Sub
End Module
|
| How To Query XML with an XPath Expression by Using Visual Basic .NET |
Use the Imports statement on the Xml
and XPath namespaces so that you are not required to qualify
declarations in those namespaces later in your code. You must use the
Imports statements prior to any other declarations.Imports System.Xml Imports System.Xml.XPathDeclare the appropriate variables. Declare an XPathDocument object to hold the XML document, an XpathNavigator object to evaluate XPath expressions, and an XPathNodeIterator object to iterate through selected nodes. Declare a String object to hold the XPath expressions. Add the declaration code in the Main procedure in Module1. Dim nav As XPathNavigator Dim docNav As XPathDocument Dim NodeIter As XPathNodeIterator Dim strExpression As StringLoad an XPathDocument with the sample file Books.xml. The XPathDocument class uses Extensible Stylesheet Language Transformations (XSLT) to provide a fast and performance-oriented cache for XML document processing. It is similar to the XML Document Object Model (DOM) but is highly optimized for XSLT processing and the XPath data model. 'Open the XML.
docNav = New XPathDocument("books.xml")
Create an XPathNavigator from the
document. The XPathNavigator object is used for read-only XPath
queries. The XPath queries may return a resulting value or many nodes.'Create a navigator to query with XPath. nav = docNav.CreateNavigatorCreate an XPath expression to find the average cost of a book. This XPath expression returns a single value. For full details on XPath syntax, see "XPath Syntax" in the "References" section. 'Find the average cost of a book. 'This expression uses standard XPath syntax. strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)"Use the Evaluate method of the XPathNavigator object to evaluate the XPath expression. The Evaluate method returns the results of the expression. 'Use the Evaluate method to return the evaluated expression.
Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression))
Create an XPath expression to find all of the
books that cost more than ten dollars. This XPath expression returns
only Title nodes from the XML source.'Find the title of the books that are greater than 10 dollars. strExpression = "/bookstore/book/title[../price>10.00]"Create an XPathNodeIterator for the nodes that are selected with the Select method of the XPathNavigator. The XPathNodeIterator represents an XPath nodeset and therefore supports operations on this nodeset. 'Select the node and put the results into an iterator. NodeIter = nav.Select(strExpression)Use the XPathNodeIterator, which was returned from the Select method of XPathNavigator, to move through the selected nodes. In this case, you can use the MoveNext method of the XPathNodeIterator to iterate through all of the selected nodes. Console.WriteLine("List of expensive books:")
'Iterate through the results that show the element value.
While (NodeIter.MoveNext())
Console.WriteLine("Book Title: {0}", NodeIter.Current.Value)
End While
Use the ReadLine method to add a pause
at the end of the console display to more readily display the above
results.'Pause. Console.ReadLine()Build and run your project. Note that the results are displayed in the console window. |