?
Solved

XML & VB.NET

Posted on 2003-02-26
4
Medium Priority
?
449 Views
Last Modified: 2012-06-21
I have the following XML file:

<?xml version="1.0" encoding="utf-8" ?>
<NOTICEBOARD>
     <NOTICE area="NEWS">
          <DATE>Some Date</DATE>
          <TIME>Some Time</TIME>
          <DESCRIPTION>dfgdfgdfg dfg sfdg dfg</DESCRIPTION>
          <DOC_ID>1</DOC_ID>
     </NOTICE>
     <NOTICE area="LATEST">
          <DATE>Some Date</DATE>
          <TIME>Some Time</TIME>
          <DESCRIPTION>d fg dfg dfsg dfgfdg fdgdfg dfg</DESCRIPTION>
          <DOC_ID>2</DOC_ID>
     </NOTICE>
     <NOTICE area="OTHER">
          <DATE>Some Date</DATE>
          <TIME>Some Time</TIME>
          <DESCRIPTION>sf ertw agt sgdfg </DESCRIPTION>
          <DOC_ID>3</DOC_ID>
     </NOTICE>
</NOTICEBOARD>

What I want to do is loop thru the xml file and select nodes with particular attribute values, and then read the node values that have that attribute.

And I have some code to traverse the file:

Dim objXMLReader As XmlTextReader = New XmlTextReader("c:\Noticeboard.xml")
        While objXMLReader.Read()
            Dim area As String = objXMLReader.GetAttribute("area")
            MessageBox.Show(isbn)
            If area = "NEWS" Then
               
           'WHAT GOES HERE TO EXTRACT THE NODE VALUES WHEN THE ATTRIBUTE = NEWS



            End If
        End While
0
Comment
Question by:bLUE
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 400 total points
ID: 8026155
There is no need to do search your self. Let XPath query do it for you. Here is the code that you can use..

Sub Main()
        Dim obDoc As XmlDocument = New XmlDocument()
        obDoc.Load("DataFile.xml")

        Dim strQuery As String = "//*[@area='NEWS']"
        Dim nodeList As XmlNodeList = obDoc.SelectNodes(strQuery)
        Dim notice As XmlNode
        For Each notice In nodeList
            Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").InnerText, notice("DOC_ID").InnerText)

        Next
    End Sub
0
 
LVL 1

Author Comment

by:bLUE
ID: 8026536
Naveenkohli thanks.

Just a side question. From the XML I have 3 different values of the attritubte "area". By looping thru the xml file how would I do something like..

select case (areas attribute value)
case "NEWS"
   'then read the node values and do stuff
   'how would i get the innerText values out
case "OTHER"
   'then read the node values and do stuff
case "LATEST"
   'then read the node values and do stuff
End Select

Thanks very much
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 8026661
If you are thinking of reading all the nodes, here is one appraoch that you can take..

Dim docNode As XmlNode = obDoc.DocumentElement
        For Each notice In docNode
            ' Get the area attribute
            Dim strArea As String = notice.Attributes("area").InnerText
            Select Case (strArea)
                Case "NEWS"
                    Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").InnerText, notice("DOC_ID").InnerText)
                Case "LATEST"
                    Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").InnerText, notice("DOC_ID").InnerText)
                Case "OTHER"""
                    Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").InnerText, notice("DOC_ID").InnerText)
            End Select
        Next
0
 
LVL 1

Author Comment

by:bLUE
ID: 8026699
Thank you very much.. !! Much appreciated
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

578 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