Jump to content

Pulling Looped XML Data Into ASP.NET Page


kwilliams

Recommended Posts

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):

<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:

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: openobjShowAlert: True<h1>SHOW ALERT</h1>WANTED RESULT:*Page Properties*strAlertStatus: openstrAlertState: IA <<----THIS IS WHAT I NEEDobjShowAlert: True<h1>SHOW ALERT</h1>

Link to comment
Share on other sites

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

<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:

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 IfEnd Sub

RESULT:*Page Properties*strAlertIssued: 01/01/2008strAlertUpdated: 01/01/2008 7:00 AM*Page Properties*strAlertIssued: 01/01/2008strAlertUpdated: 01/01/2008 8:00 AM*Page Properties*strAlertIssued: 01/01/2008strAlertUpdated: 01/01/2008 9:00 AMThis 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.

Link to comment
Share on other sites

It looks like you are using this to get the nodes:

alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")alert_status = alert_xmld.SelectNodes("/alerts/alert/status")alert_state = alert_xmld.SelectNodes("/alerts/alert/state")

But the elements in your XML are like this:

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

You might try changing your VB to something like this:

alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")alert_status = alert_xmld.SelectNodes("/alerts/alert/alertstatus")alert_state = alert_xmld.SelectNodes("/alerts/alert/alertstate")

Link to comment
Share on other sites

It looks like you are using this to get the nodes:
alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")alert_status = alert_xmld.SelectNodes("/alerts/alert/status")alert_state = alert_xmld.SelectNodes("/alerts/alert/state")

But the elements in your XML are like this:

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

You might try changing your VB to something like this:

alert_nodelist = alert_xmld.SelectNodes("/alerts/alert")alert_status = alert_xmld.SelectNodes("/alerts/alert/alertstatus")alert_state = alert_xmld.SelectNodes("/alerts/alert/alertstate")

Hello Jesh,I apologize about that, but that was just a typo. It really reads:
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:

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:

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:

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: closedstrAlertState: CO*Page Properties*strAlertStatus: closedstrAlertState: CO*Page Properties*strAlertStatus: closedstrAlertState: COI 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 trying really hard to solve this on my own, but I REALLY need some help on this one. Any other suggestions would be greatly appreciated. Thanks.

Link to comment
Share on other sites

It sounds to me like you were on the right track with this snippet:

strAlertStatus = alert_status.Attributes.GetNamedItem("status").Value

That error message is indicating that the "alert_status" variable is an XmlNodeList - a collection of XmlNodes - rather than an XmlNode.If you iterate through that collection, you should be able to get at each of the alertstatuses.This page might help:http://msdn2.microsoft.com/en-us/library/hctcaayx.aspx

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...