Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-10-17
5
Medium Priority
?
169 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: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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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