Jump to content

ASP.NET XML Feed Error


kwilliams

Recommended Posts

I have an ASP.NET page that parses an external XML feed (please see code below). It works...but when the feed goes down, it subsequently brings my site down. So I've created a default XML document that's located on my internal server a backup method when/if the external feed goes down. But I have no idea how to do this. If someone could let me know how to accomplish this, or point me in the right direction, that would be great. Thanks in advance.ASP.NET XML FEED CODE

'Check for active amber alertDim strAlertTitle As String, strAlertDescription As StringDim intAlertCounter As Integer = 0Dim alert_xmld As XmlDocumentDim alert_nodelist As XmlNodeListDim alert_node As XmlNode'Create the XML Documentalert_xmld = New XmlDocument()'Load the Xml filealert_xmld.Load("http://codeamber.org/a1xl04act/amberalert.xml") 'LIVE		'alert_xmld.Load("http://www.mysite.com/docs/xml/alert.xml") 'DEFAULT XML DOC TO USE AT DOWNTIME'Get the list of name nodesalert_nodelist = alert_xmld.SelectNodes("/AmberAlerts/AmberAlert[Alertstatus/@status = 'open' and contains(Alertinfo/@states, 'CO')]")		'Loop active amber alertsFor Each alert_node In alert_nodelist	intAACounter = intAACounter + 1	If intAACounter >= 1 Then		strAlertTitle = "Amber Alert"		strAlertDescription = "There is currently an amber alert in Colorado."	Else If intAACounter = 0 Then		strAlertTitle = "Amber Alert"		strAlertDescription = "There are currently no amber alerts in Colorado."	End IfNext 'end loopResponse.Write(strAlertTitle & ": " & strAlertDescription & "<br /><br />")

Link to comment
Share on other sites

I have an ASP.NET page that parses an external XML feed (please see code below). It works...but when the feed goes down, it subsequently brings my site down. So I've created a default XML document that's located on my internal server a backup method when/if the external feed goes down. But I have no idea how to do this. If someone could let me know how to accomplish this, or point me in the right direction, that would be great. Thanks in advance.
Probably needs some exception handling to solve this. When you say it brings your site down, is it throwing an exception when trying to load/use that xml document? Add a try/catch around that code, and code your default xml load in the catch.
Link to comment
Share on other sites

Probably needs some exception handling to solve this. When you say it brings your site down, is it throwing an exception when trying to load/use that xml document? Add a try/catch around that code, and code your default xml load in the catch.
Thanks Reg Edit,Could you give me a quick example of a try/catch using my code? I'd appreciate it.
Link to comment
Share on other sites

Thanks Reg Edit,Could you give me a quick example of a try/catch using my code? I'd appreciate it.
OK, but as I say, if it brings your site down, is it throwing an exception, because if so, you need to look at the details of that exception in case there's something you need to handle better in your code. For instance, I can see that if your For loop exits with no alerts, then two uninitialised string variables get used, which would cause a null reference exception. So you should initialise those two when you declare them:
Dim strAlertTitle As String = String.Empty, strAlertDescription As String = String.Empty

Anyway, if you do use a try/catch then it would look something like:

Try	' load live xml hereCatch ex As Exception	' live load threw an exception: load the default xml hereEnd Try

Link to comment
Share on other sites

OK, but as I say, if it brings your site down, is it throwing an exception, because if so, you need to look at the details of that exception in case there's something you need to handle better in your code. For instance, I can see that if your For loop exits with no alerts, then two uninitialised string variables get used, which would cause a null reference exception. So you should initialise those two when you declare them:
Dim strAlertTitle As String = String.Empty, strAlertDescription As String = String.Empty

Will do. I was thinking the same thing. I appreciate the info.
Anyway, if you do use a try/catch then it would look something like:
Try	' load live xml hereCatch ex As Exception	' live load threw an exception: load the default xml hereEnd Try

Thanks for the example Try/Catch. It's kind of a pain being a newbie, so I appreciate your help.
Link to comment
Share on other sites

OK, but as I say, if it brings your site down, is it throwing an exception, because if so, you need to look at the details of that exception in case there's something you need to handle better in your code. For instance, I can see that if your For loop exits with no alerts, then two uninitialised string variables get used, which would cause a null reference exception. So you should initialise those two when you declare them:
Dim strAlertTitle As String = String.Empty, strAlertDescription As String = String.Empty

Ok, I tried your suggestion. But when I changed "As String" to "As String.Empty" on the two variable declarations, I received the following error:Compiler Error Message: BC30205: End of statement expected.Source Error:Line 561: Dim strAlertTitle As String.Empty, strAlertDescription As String.EmptyAny ideas on why I received that error?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...