?
Solved

XML & VB.NET

Posted on 2003-02-26
4
Medium Priority
?
440 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
[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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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 this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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