VB.NET - How to read a XML file using VB Net - Asked By Lennie Kuah on 04-Sep-05 06:47 AM
Hi All,
I am a newbie to Vb.Net. I am trying to read from XML file and load the element text (first name & " " & last name) into a listbox but have no joy. Could someone please help me out. Thanks.
Cheers,
Lennie
.....................................................................................................................................
Framents of Books.xml
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<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>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore
------------------------------------------------------------------------------------------------------
My scripts
Imports system.IO
Imports system.xml
Imports ReadXML
Dim strDir As String = "C:\EDITemp\Books.xml"
Dim xReader as XmlTextReader = new XmlTextReader(strDir)
Do while xReader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element ' node is an element
If reader.AttributeCount > 0 Then
While reader.MoveToNextAttribute()
If XmlNodeType.Text Then
Listbox1.items.add ( reader.Value)
End If
End While
End If
End Select
Loop
I would recommend using an XmlDocument - Asked By Jon Wojtowicz on 04-Sep-05 10:18 AM
and getting a NodeList from an Xpath expression. You can thn perform your logic on the child nodes of the nodes in the NodeList
Dim doc As XmlDocument = New XmlDocument
doc.Load("C:\EDITemp\Books.xml")
Dim nodes As XmlNodeList = doc.SelectNodes("//author")
For Each node As XmlNode In nodes
' Process the child nodes, first-name, last-name and name
Next
read an xml file using vb net - Asked By Sunitha Nambari on 04-Sep-05 11:55 PM
hi,
Here is the code:
// Reading an xml file
Sub ReadXML(FileName as String)
Try
lblXMLFile.Text =""
Dim objxml as new XMLTextReader(FileName)
dim sCategory as String
dim bNested as Boolean
dim sLastElement as String
Dim sValue as String
'Read method loops through the XML stream
Do While objxml.Read
'Output elements and values
'Look at output in browser and compare to menu.xml file to
'see exactly what is being done
If objxml.NodeType = XMLNodeType.Element Then
if bNested = True then
if sCategory <> "" then sCategory = sCategory & " > "
sCategory = sCategory & sLastElement
End if
bNested = True
sLastElement = objxml.Name
Else If obhxml.NodeType = XMLNodeType.Text or _
objxml.NodeType = XMLNodeType.CData Then
bNested = False
sCategory = "<P>" & sCategory
sValue = objxml.value
lblXMLFile.Text = lblXMLFile.Text & "<B>" & sCategory & _
"<BR>" & sLastElement & "</B><BR>" & sValue
sLastElement = ""
sCategory = ""
End if
Loop
objxml.close
Catch Ex as Exception
lblXMLFile.Text = "The following error occurred: " & Ex.Message
End Try
End Sub
Regards,
Suneetha
Use an XmlDocument - Asked By Aarthi Saravanakumar on 05-Sep-05 10:53 AM
or XPathNavigator to do this. Your XML is small and these kind of searches are quicker in DOM. Load your XML into an XMLDocument and select the author node and then parse the child nodes as in Jon's example
Read an XML file using vb net - Asked By Lennie Kuah on 06-Sep-05 04:22 AM
Hi Suneetha,
Thank you for taking time to help me out. Appreciate your help very much.
Cheers,
Lennie
Read xml file using Vb Net - Asked By Lennie Kuah on 06-Sep-05 04:23 AM
Hi Aarthi,
Thanks for all you advice and help. You are awesome to help me out.
Cheers,
Lennie