Solved

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

Posted on 2004-10-17
5
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12336509
       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
ID: 12336579
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
ID: 12337684
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
ID: 12340200
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
ID: 12344617
plz add the following Imports on top of your .vb page:
Imports System.IO
Imports System.Xml
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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