+ Reply to Thread
Results 1 to 4 of 4

Thread: Read XML using VB.NET

  1. #1

    Default Read XML using VB.NET

    This tutorial will show you how to read and extract data from an XML document with VB.NET using the XmlTextReader class in the System.Xml namespace. To view the article on how to write XML documents, please click here

    The XML classes in the System.Xml namespace that can be used to read XML documents are:
    • XmlReader
    • XmlTextReader
    • XmlNodeReader
    • There are more readers like XmlDictionaryReader and XmlValidatingReader, but they don’t apply to this tutorial

    The code below will provide you with most of the functionality needed to extract data from an XML document. Usually this type of code is used to extract data from an XML RSS feed after which it can be imported to a database. As with writing an XML document (XmlTextWriter and XmlWriter), XmlTextReader and XmlNodeReader are inherited from XmlReader which means XmlReader has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextReader (or XmlNodeReader).

    Code:
    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Xml" %>
    <script runat="server">
    Sub Page_Load(Src As Object, E As EventArgs)
    	
        '*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
        '*** http://forum.coolwebawards.com/
    	
    	'*** XmlTextReader
    	Dim XmlReader As New XmlTextReader(server.MapPath("test.xml"))
    
    	'*** Uncomment to use XmlReader
    	'Dim XmlReader As XmlReader = XmlReader.Create(server.MapPath("test.xml"))
    
    	'*** Uncomment to use XmlNodeReader
    	'Dim XMLDocument = New XmlDocument
    	'XMLDocument.Load(server.MapPath("test.xml"))
    	'Dim XmlReader = New XmlNodeReader(XMLDocument)
    
    	Dim intID, intProductCountTotal As Integer
    	Dim strPrice, strWeight, strDescription As String
    	
    	While XmlReader.Read()
    			
    		'extract the atribute "Id" value when the reader reach the "Product" element.
    		If XmlReader.Name.ToString() = "Product" Then 
    			If XmlReader.HasAttributes Then
    				While XmlReader.MoveToNextAttribute()
    					If XmlReader.Name = "Id" Then
    						intID = cint(XmlReader.Value)
    					End If
    				End While
    			End If			
    		End If
    		If XmlReader.Name.ToString() = "price" Then strPrice = trim(XmlReader.ReadString())
    		If XmlReader.Name.ToString() = "weight" Then strWeight = trim(XmlReader.ReadString())
    		If XmlReader.Name.ToString() = "description" Then strDescription = trim(XmlReader.ReadString())
    
    		'When the end of each <Product> element is reached by the XmlReader, you can extract all the values you need and import them into a database or whatever
    		'These readers provide forward-only access to XML data, so I choose to extract the data from each of the elements, and then use the values
    		If XmlReader.Name.ToString() = "Product" AND XmlReader.NodeType.ToString() = "EndElement" Then 
    			
    			response.write("Values at product ID " & cstr(intID) & " is:<br>")
    			response.write("- price: " & strPrice & "<br>")
    			response.write("- weight: " & strWeight & "<br>")
    			response.write("- description: " & strDescription & "<br>")
    			
    			'Reset the values for next product
    			intID = 0
    			strPrice = ""
    			strWeight = ""
    			strDescription = ""
    			
    			intProductCountTotal += 1
    
    		End If
    		
    	End While
    
    	XmlReader.Close()
    	
    	' print the number of <Products> elements counted
    	response.write("<br>Products in XML doc: " & cstr(intProductCountTotal))
    	
    End Sub
    </script>
    The XML document info used:

    Code:
    <!--http://forum.coolwebawards.com-->
    <Products>
        <Product Id="1">
            <price>price element text for ID 1</price>
            <weight>weight element text for ID 1</weight>
            <description>description element text for ID 1</description>
    	</Product>
        <Product Id="2">
            <price>price element text for ID 2</price>
            <weight>weight element text for ID 2</weight>
            <description>description element text for ID 2</description>
    	</Product>
        <Product Id="3">
            <price>price element text for ID 3</price>
            <weight>weight element text for ID 3</weight>
            <description>description element text for ID 3</description>
    	</Product>
    </Products>
    The result when reading the document

    Code:
    Values at product ID 1 is:
    - price: price element text for ID 1
    - weight: weight element text for ID 1
    - description: description element text for ID 1
    Values at product ID 2 is:
    - price: price element text for ID 2
    - weight: weight element text for ID 2
    - description: description element text for ID 2
    Values at product ID 3 is:
    - price: price element text for ID 3
    - weight: weight element text for ID 3
    - description: description element text for ID 3
    
    Products in XML doc: 3
    Click here for more information on how to write an XML document using vb.net
    CWA - CoolWebAwards.com

  2. #2
    Sapphire Star member vjackcon is on a distinguished road
    Join Date
    Feb 2011
    Posts
    51

    Default

    Load an XML file as data using PHP

    By handling mime-types and using browser detection, CodeHelp has already shown how to export XML using a PHP script. PHP can also receive XML as input - using the XML parser:

    if (!($fp=@fopen("./contactsbare.xml", "r")))
    die ("Couldn't open XML.");
    $usercount=0;
    $userdata=array();
    $state='';
    if (!($xml_parser = xml_parser_create()))
    die("Couldn't create parser.");

  3. #3
    Ruby Star member nobita_deptrai is on a distinguished road
    Join Date
    Jun 2011
    Posts
    458

    Default

    Thanks for the response yea I know its on me to learn that why I usually dont like posting so much but sometimes i just get so frustrated from not understanding what I am reading that I end up deciding to post but in the mean time while im waiting for a response I still continue to try.


    Now I understand a lot more than i ever did about xml files and using vb to read them.

    I was able to get the entire xml file to show up But I am not to sure how to approach getting it to format into a table using html.

    Maybe you could help with shedding some light on that I would be grateful

    Thanks
    BDS Real Estate Company
    Go to my website rao vat ban nha to learn about real estate in Vietnam: Home sales , Apartment for sale, Feng Shui ...

  4. #4

    Default

    Thanks for the sharing I know its on me to learn that why I usually dont like posting so much but sometimes i just get so frustrated from not understanding what I am reading that I end up deciding to post but in the mean time while i am waiting for a response I still continue to try.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts