[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-17
5
Medium Priority
?
168 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:Fahad Mukhtar
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:Fahad Mukhtar
Fahad Mukhtar earned 1000 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 1000 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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 …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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