Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2008, 06:55 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pulling Looped XML Data Into ASP.NET Page

I have an ASP.NET page w/ VB syntax that loops through an XML document's nodes, and returns values from the selected node's attribute (status).

Now in addition to the "status" attribute located in the "alertstatus" node, I need to pull the "state" attribute from the "alertstate" node that's located within the main element titled "<alert>". But I'm not sure how to do this. I've included the code that I have so far below. If anyone can clue me in on how I can accomplish this, that would be great. Thanks for any and all help.

XML DOC (alerts.xml):
Code:
<alerts>
    <alert issued="01/12/2008" updated="01/12/2008 7:00 AM">
        <alertstatus status="closed"></alertstatus>
        <alertstate state="CO"></alertstate>
    </alert>

    <alert issued="01/13/2008" updated="01/13/2008 8:00 AM">
        <alertstatus status="open"></alertstatus>
        <alertstate state="IA"></alertstate>
    </alert>

    <alert issued="01/14/2008" updated="01/14/2008 9:00 AM">
        <alertstatus status="closed"></alertstatus>
        <alertstate state="KS"></alertstate>
    </alert>
</alerts>
ASP.NET DOC:
Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
       Dim objNodeExists As Object = False
        'Check for active alert
        Dim alert_xmld As XmlDocument
        Dim alert_nodelist As XmlNodeList
        Dim alert_status As XmlNodeList, alert_state As XmlNodeList
        Dim strAlertStatus As String, strAlertState As String
    Dim alert_node As XmlElement
        Dim objShowAlert As Object = False
        'Create the XML Document
        alert_xmld = New XmlDocument()
        'Load the Xml file
    alert_xmld.Load("http://www.mysite.com/docs/xml/alerts.xml") 'obviously this is not a real live document:)

        'Get the list of name nodes
        alert_nodelist = alert_xmld.SelectNodes("/alerts/alert/alertstatus")

    For Each alert_node In alert_nodelist

            If objShowAlert = False Then
                'Assign object if node is not empty
                objNodeExists = True

        'Get an Attribute Value
        strAlertStatus = alert_node.Attributes.GetNamedItem("status").Value

                If strAlertStatus = "open" Then
                    objShowAlert = True
                End If

                'Write TEXT results to the page - TEST
                Response.Write("<strong>*Page Properties*</strong><br />" & _
                "strAlertStatus: " & strAlertStatus & "<br />" & _
                "objShowAlert: " & objShowAlert & "<br /><br />")
            End If

        Next 'end loop

    'Alert show/hide
    If objShowAlert = True Then
        Response.Write("<h1>SHOW ALERT</h1>")
    End If

    End Sub



RESULT:

*Page Properties*
strAlertStatus: open
objShowAlert: True

<h1>SHOW ALERT</h1>


WANTED RESULT:

*Page Properties*
strAlertStatus: open
strAlertState: IA <<----THIS IS WHAT I NEED
objShowAlert: True

<h1>SHOW ALERT</h1>

KWilliams
 
Old January 18th, 2008, 06:50 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I figured out how to loop this XML document until a condition is reached:

XML:
Code:
<alerts>
    <alert issued="01/02/2008" updated="01/02/2008 7:00 AM">
        <alertstatus status="closed"></alertstatus>
        <alertstate state="CO"></alertstate>
    </alert>

    <alert issued="01/02/2008" updated="01/02/2008 8:00 AM">
        <alertstatus status="open"></alertstatus>
        <alertstate state="IA"></alertstate>
    </alert>

    <alert issued="01/02/2008" updated="01/02/2008 9:00 AM">
        <alertstatus status="closed"></alertstatus>
        <alertstate state="KS"></alertstate>
    </alert>
</alerts>
ASP.NET:
Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim objNodeExists As Object = False
        'Check for active alert
        Dim alert_xmld As XmlDocument
        Dim alert_nodelist As XmlNodeList
        Dim alert_status As XmlNodeList, alert_state As XmlNodeList
        Dim strAlertStatus As String, strAlertState As String
    Dim alert_node As XmlElement
        Dim objShowAlert As Object = False
        'Create the XML Document
        alert_xmld = New XmlDocument()
        'Load the Xml file
        alert_xmld.Load("alerts.xml")

        'Get the list of name nodes
        alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")
        alert_status = alert_xmld.SelectNodes("/alerts/alert/status")
        alert_state = alert_xmld.SelectNodes("/alerts/alert/state")

    Dim intCounter As Integer = 0
    Dim strAlertIssued As String, strAlertUpdated As String
    If objShowAlert = False Then
        For Each alert_node In alert_nodelist
            intCounter = intCounter + 1
            If strAlertIssued = "01/02/2008" AND intCounter <= 10 Then
                'Assign object if node is not empty
                objNodeExists = True

                'Get an Attribute Value
                strAlertIssued = alert_node.Attributes.GetNamedItem("issued").Value
                strAlertUpdated = alert_node.Attributes.GetNamedItem("updated").Value

                If strAlertIssued = "01/02/2008" Then
                    objShowAlert = True
                End If

                Response.Write("<strong>*Page Properties*</strong><br />" & _
                "strAlertIssued: " & strAlertIssued & "<br />" & _
                "strAlertUpdated: " & strAlertUpdated & "<br /><br />")
            End If
        Next 'end loop
    End If

    'Alert show/hide
        If objShowAlert = True Then
            Response.Write("<h1>SHOW ALERTS</h1>")
        End If
End Sub
RESULT:
*Page Properties*
strAlertIssued: 01/01/2008
strAlertUpdated: 01/01/2008 7:00 AM

*Page Properties*
strAlertIssued: 01/01/2008
strAlertUpdated: 01/01/2008 8:00 AM

*Page Properties*
strAlertIssued: 01/01/2008
strAlertUpdated: 01/01/2008 9:00 AM

This works great if I'm just trying to pull the attributes ("issued" and "updated") from the "alert" node, but it doesn't work when I try to pull values from child nodes of the "alert" node (@status from alertstatus node and @state from alertstate node).

I've tried messing around with different methods (i.e. GetElementsByTagName, SelectSingleNode, etc.), but I'm a newbie to this, and I'm not sure how to accomplish what I need using these methods.

If anyone can please help me to figure out what I'm doing wrong, I'd really appreciate it. Thanks again.

KWilliams
 
Old January 22nd, 2008, 11:34 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A typo on my last post was pointed out to me, so I wanted to make note of it. This code:
Code:
alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")
alert_status = alert_xmld.SelectNodes("/alerts/alert/status")
alert_state = alert_xmld.SelectNodes("/alerts/alert/state")
actually reads like this:
Code:
alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")
alert_status = alert_xmld.SelectNodes("/alerts/alert/alertstatus")
alert_state = alert_xmld.SelectNodes("/alerts/alert/alertstate")
I had to change some of the node names for the post, and I forgot to change those ones. Anyway, I think that my problem lies in the call of the node. Here's what I've tried so far:

Using this:
Code:
strAlertStatus = alert_node.Attributes.GetNamedItem("status").Value
                strAlertState = alert_node.Attributes.GetNamedItem("state").Value

results in the following error for the "strAlertStatus" node:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Using this:
Code:
strAlertStatus = alert_status.Attributes.GetNamedItem("status").Value
                strAlertState = alert_state.Attributes.GetNamedItem("state").Value

results in the following error for the "strAlertStatus" node:
 Compiler Error Message: BC30456: 'Attributes' is not a member of 'System.Xml.XmlNodeList'.

And using this:
Code:
strAlertStatus = aa_node.SelectSingleNode("//alertstatus/@status").Value
                    strAlertState = aa_node.SelectSingleNode("//alertstate/@state").Value

Results in the first nodeset being looped three times, which is the number of nodesets in the XML doc, like this:
*Page Properties*
strAlertStatus: closed
strAlertState: CO

*Page Properties*
strAlertStatus: closed
strAlertState: CO

*Page Properties*
strAlertStatus: closed
strAlertState: CO

I guess this could be progress, but I obviously don't know why it's not looping properly. So as you can see, I'm pretty lost on where to go from here. I'm really trying hard to solve this on my own, but I'd REALLY appreciate some help. Any suggestions would be greatly appreciated. Thanks.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting XML data in a dropdown list on asp.net Hughesie78 XML 4 November 27th, 2007 11:50 AM
asp.net webpage pulling sp_who2 help!!!! zombie212 ASP.NET 1.0 and 1.1 Basics 2 August 16th, 2007 07:28 AM
Parse XML doc using VB.NET into ASP.NET page kwilliams Classic ASP XML 17 November 11th, 2005 10:42 AM
using data filtered from asp.net page to generate ashutoshpandey31 Crystal Reports 0 March 12th, 2005 09:40 AM
Pulling data from an Access DB onto an ASP page Melissa Brown Classic ASP Databases 7 August 6th, 2004 09:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.