bLUE
asked on
XML & VB.NET
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:\Noticebo ard.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
<?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:\Noticebo
While objXMLReader.Read()
Dim area As String = objXMLReader.GetAttribute(
MessageBox.Show(isbn)
If area = "NEWS" Then
'WHAT GOES HERE TO EXTRACT THE NODE VALUES WHEN THE ATTRIBUTE = NEWS
End If
End While
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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").Inne rText, 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").Inne rText, 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").Inne rText, notice("DOC_ID").InnerText )
End Select
Next
Dim docNode As XmlNode = obDoc.DocumentElement
For Each notice In docNode
' Get the area attribute
Dim strArea As String = notice.Attributes("area").
Select Case (strArea)
Case "NEWS"
Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").Inne
Case "LATEST"
Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").Inne
Case "OTHER"""
Console.WriteLine("Date: {0}, Time:{1},Description:{2}, Doc_Type:{3}", notice("DATE").InnerText, notice("TIME").InnerText, notice("DESCRIPTION").Inne
End Select
Next
ASKER
Thank you very much.. !! Much appreciated
ASKER
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