Hi,

I am wondering what I might be doing wrong here regarding getting the XML Attribute of my xml file, here is a snippet:

<music_songs>
  <song>
  <title>(I Just) Died In Your Arms</title>
  <date added="03-24-2009" />
</song>
</music_songs>

Here is my code,

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
Sub Page_Load() 

  Dim file As String = Context.Server.MapPath("alice_music_list2.xml") 
  Dim document As XmlDocument = New XmlDocument() 
  document.Load(file) 

  Dim songList As XmlNodeList 
  songList = document.SelectNodes("/music_songs/song/category[text()='Rock']")  

  Dim song As XmlNode
  For Each song In songList
    
    Dim title As XmlNode = song.FirstChild
    songNodesOut.Text &=  "<b>Title:</b> " & title.InnerText & "<br/>"

    Dim date AS XmlNode = title.NextSibling
    Dim dateAttribute AS attribute= date.Attributes.GetNamedItem("added").Value
    SongNodesOut.Text &= "<b>Date Added to Collection</b> " & dateAttribute & "<br/><hr/><br />"
   
   Next

  }
		
end Sub
</script>

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30183: Keyword is not valid as an identifier.

Source Error:

Line 27: Dim date AS XmlNode = title.NextSibling
Line 28: Dim dateAttribute AS attribute= date.Attributes.GetNamedItem("added").Value
Line 29: SongNodesOut.Text &= "<b>Date Added to Collection</b> " & dateAttribute & "<br/><hr/><br />"

Is this not the way for me to declare attributes?
Thanks for your help.

Date is a VB.NET reserved name.

Dim date1 As XmlNode = title.NextSibling
            
 Dim dateAttribute As XmlAttribute = date1.Attributes("added")
 SongNodesOut.Text &="<br/>" & dateAttribute.Value)

Thanks for the snippet, but I get in my error now, Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Here is my entire subroutine:

Sub Page_Load() 

  Dim file As String = Context.Server.MapPath("music_list2.xml") 
  Dim document As XmlDocument = New XmlDocument() 
  document.Load(file) 

  Dim songList As XmlNodeList 
  songList = document.SelectNodes("/music_songs/song/category[text()='Rock']")  

  Dim song As XmlNode
  For Each song In songList
    
    Dim title As XmlNode = song.FirstChild
    songNodesOut.Text &=  "<b>Title:</b> " & title.InnerText & "<br/>"
  
    Dim date1 AS XmlNode = title.NextSibling
    Dim dateAttribute As XmlAttribute = date1.Attributes("added")
    SongNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/><br />"
   
  Next		
end Sub

Could there be another issue here that I am not aware of?
Thanks for your help.

Please verify whether an element has an attribute or not.

Sub Page_Load() 

  Dim file As String = Context.Server.MapPath("music_list2.xml") 
  Dim document As XmlDocument = New XmlDocument() 
  document.Load(file) 

  Dim songList As XmlNodeList 
  songList = document.SelectNodes("/music_songs/song[@category='Rock']")  

  Dim song As XmlNode
  For Each song In songList
    
    Dim title As XmlNode = song.FirstChild
    songNodesOut.Text &=  "<b>Title:</b> " & title.InnerText & "<br/>"
  
    Dim date1 AS XmlNode = title.NextSibling
    If date1.Attributes.Count<>0 then
       Dim dateAttribute As XmlAttribute = date1.Attributes("added")
       SongNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/><br />"
    End IF   
  Next		
end Sub

To answer your question, yes. However, I found something else, I finally decided to test it out by snipping off almost everything in my selects except keeping my

Dim title As XmlNode = song.FirstChild
    songNodesOut.Text &=  "<b>Title:</b> " & title.InnerText & "<br/>"

Yet, this is what I got on my screen without any format:
<b>Title:</b> Rock<br/>.

So, it looks like that it has selected only Rock, when that is supposed to be the category of the song I would like to pick. Since there are no children after that, no wonder it did not bring anything back and brought me null. I changed the xpath expression for songList to

songList = document.SelectNodes("/music_songs/song[category='Rock']/*")

But, I still get an error that says

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I have 2 questions here,

1) Why is it that I don't get the HTML format, but only the tags on my screen?
2) I want to select nodes that has the category that is "Rock", what do I need to do in my title node to make sure everything is selected?

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.