Performing a conditional on an XML Node Value (ASP.NET VB.NET)

H

hammad

Hello, I have a very urgent help request and if anybody can help me
out with it it would be very much appreciated.

I am using WebClient to inititate a URL request. This request returns
an XML string and I need to lookup one of the values within the XML
and perform a conditional statement on it.

I can get my XML response ok and have gotten as far as loading it into
a Dataset and rendering it too. However, I'm stumped on how to
actually look at the individual nodes/objects within the XML and
perform an action on it depending on the value.

The XML response I am working with is:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Status>XMLINVALID</Status>
<ErrorMessage>XML Empty</ErrorMessage>
</Response>

I need to perform the action on the <ErrorMessage> block (redirect
users depending on the value).

Not sure if I need to perform this on the dataset, or on the XML
itself (XMLTextReader)

Help and advice very much appreciated for this now very urgent
problem!

Many thanks in advance and apologies for any annoyance.
 
R

rowe_newsgroups

Hello, I have a very urgent help request and if anybody can help me
out with it it would be very much appreciated.

I am using WebClient to inititate a URL request. This request returns
an XML string and I need to lookup one of the values within the XML
and perform a conditional statement on it.

I can get my XML response ok and have gotten as far as loading it into
a Dataset and rendering it too. However, I'm stumped on how to
actually look at the individual nodes/objects within the XML and
perform an action on it depending on the value.

The XML response I am working with is:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Status>XMLINVALID</Status>
<ErrorMessage>XML Empty</ErrorMessage>
</Response>

I need to perform the action on the <ErrorMessage> block (redirect
users depending on the value).

Not sure if I need to perform this on the dataset, or on the XML
itself (XMLTextReader)

Help and advice very much appreciated for this now very urgent
problem!

Many thanks in advance and apologies for any annoyance.

Do you have to load it into a dataset? I prefer to use the XmlDocument
for processing a Xml file - you can use the SelectSingleNode method
and pass an XPath to get the appropriate node. Here's a simple console
app you can play around with the XmlDocument and XmlNode classes.
(console apps are better sandboxes than web apps :))

/////////////////////////
Imports System.Xml

Module Module1

Sub Main()
Dim xml As String = "<?xml version=""1.0"" encoding=""UTF-8""?
"<Response>" & _
"<Status>XMLINVALID</Status>" & _
"<ErrorMessage>XML Empty</ErrorMessage>" &
_
"</Response>"

Dim doc As New XmlDocument()
doc.LoadXml(xml)

Dim node As XmlNode = doc.SelectSingleNode("//Response//
ErrorMessage")
If node.InnerText = "XML Empty" Then
Console.Write("No error message")
Else
Console.Write("The Error message was: {0}",
node.InnerText)
End If

Console.Read()
End Sub

End Module
///////////////////

Thanks,

Seth Rowe
 
H

hammad

Thanks Seth

This is in fact the very solution I ended up redeveloping the page to
do; so glad to know it's the right way!

Many thanks for your response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top