Solved

XML / VB.NET Simple Question... 500pts!

Posted on 2004-10-17
5
158 Views
Last Modified: 2010-04-24
ok... I'm a beginer when it comes to XPATH / XML.  Here is my XML File:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

<Events>
    <Event>
        <Name>Sweetest Day @ Domaine Nightclub</Name>
        <Date>10-16-04</Date>
        <NumberOfPhotos>54</NumberOfPhotos>
    </Event>
    <Event>
        <Name>Thank You @ Sauce</Name>
        <Date>10-01-04</Date>
        <NumberOfPhotos>65</NumberOfPhotos>
    </Event>
</Events>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I'm trying to just simply read the code in my VB.NET page and set it to String Vars knowing the date such as:

Dim theDate As String = "10-16-04"
Dim EventName = [XML Event Name accoicated with the value of theDate above]
Dim NumberOfPhotos = [XML Number of photos accoicated with the value of theDate above]

Can someone please tell me how to pull those values out? thanx!!
0
Comment
Question by:Moonbathing
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Desp
Comment Utility
       Dim MyTextReader As TextReader = File.OpenText("C:\MyXML.XML")
        Dim XMLString As String = MyTextReader.ReadToEnd()


        Dim myXMLdocument As XmlDocument = New XmlDocument()
        myXMLdocument.LoadXml(XMLString)

        Dim theDate As String = "10-16-04"
        Dim EventName As String = myXMLdocument.GetElementsByTagName("Name").Item(0).InnerText
        Dim NumberOfPhotos As String = myXMLdocument.GetElementsByTagName("NumberOfPhotos").Item(0).InnerText

        Console.WriteLine(theDate & "  " & EventName & "   " & NumberOfPhotos)
0
 
LVL 19

Assisted Solution

by:Desp
Desp earned 250 total points
Comment Utility
Or perhaps you need this:

        Dim MyTextReader As TextReader = File.OpenText("C:\MyXML.XML")
        Dim XMLString As String = MyTextReader.ReadToEnd()
        Dim nList As XmlNodeList

        Dim myXMLdocument As XmlDocument = New XmlDocument()
        myXMLdocument.LoadXml(XMLString)

        Dim theDate As String = "10-16-04"
        Dim EventName As String = String.Empty
        Dim NumberOfPhotos As String = String.Empty

        nList = myXMLdocument.SelectNodes("Events/Event")
        Dim nodeCount As Integer
        Dim a As Integer

        nodeCount = nList.Count()

        If (nList.Count > 0) Then
            For a = 0 To nodeCount - 1 'if date matches
                If (myXMLdocument.SelectNodes("Events/Event/Date").Item(0).InnerText = theDate) Then
                    EventName = myXMLdocument.SelectNodes("Events/Event/Name").Item(0).InnerText
                    NumberOfPhotos = myXMLdocument.SelectNodes("Events/Event/NumberOfPhotos").Item(0).InnerText
                    Exit For
                End If
            Next

            Console.WriteLine(theDate & "  " & EventName & "   " & NumberOfPhotos)
        End If

hth,
Fahad Mukhtar

0
 
LVL 2

Accepted Solution

by:
AmanBrar earned 250 total points
Comment Utility
Hi, you can easily extract these values using XMLDocument class.

    Dim m_xmld As XmlDocument
    Dim m_nodelist As XmlNodeList
    Dim m_node As XmlNode
   
'Create the XML Document
    m_xmld = New XmlDocument()

  'Load the Xml file
    m_xmld.Load("Put File Path Here")

  'Get the list of name nodes
    m_nodelist = m_xmld.SelectNodes("/Events/Event")

  'Loop through the nodes
    For Each m_node In m_nodelist

    'Get the Name Element Value
      Dim EventName = m_node.ChildNodes.Item(0).InnerText

    'Get the Date Element Value
      Dim theDate = m_node.ChildNodes.Item(1).InnerText

    'Get the NumberOfPhotos Element Value
      Dim NumberOfPhotos = m_node.ChildNodes.Item(2).InnerText
 
    Next
0
 

Author Comment

by:Moonbathing
Comment Utility
When I complie I get a:

Compiler Error Message: BC30002: Type 'TextReader' is not defined.

Clearly cause I have imported the TextReader class... For some reason, I can't get it to work.  Can someone tell me where to put my Imports statement? and what should I Import? (if I'm using Desp solution I think it's System.IO if I'm using AmanBrar Solution then I think it's XMLDocument class)

Here is my code:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<%@ Control Language="VB" Debug="true" %>
<script runat="server">

    sub Page_Load()

        Dim MyTextReader As TextReader = File.OpenText("Events.XML")
        Dim XMLString As String = MyTextReader.ReadToEnd()
        Dim nList As XmlNodeList
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Where do I put the Imports statement and what do I Import?

0
 
LVL 2

Expert Comment

by:AmanBrar
Comment Utility
plz add the following Imports on top of your .vb page:
Imports System.IO
Imports System.Xml
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now