Solved

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

Posted on 2004-10-17
5
162 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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