Parsing XML

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Thank you in advance for any and all assistance. It is greatly appreciated.

I am working with Plimus for licensing my software. I can communicate with
the server and I'm getting responses in XML. My question is, the XML stream
that's coming back is telling if it's a successful license registration,
validation, too many installs etc. How do I capture that information and use
in my logic?

I tried:


'If responseContent = "<status>STATUS_CODE</status>" Then

' MessageBox.Show("The key you have enter is either invalid
or you misentered the key. Please try again")
' License.Text = ""
' License.Focus()
'Else
MessageBox.Show(responseContent)
'MessageBox.Show("Thank you for registering. Enjoy your
software!")
'Me.Close()
'Dim frm As Form
'frm = frmEZTechToolsIdentifier
'frm.Show()

but the code doesn't capture the XML stream. What do I need please?

--
Michael Bragg, President
eSolTec, Inc.
a 501(C)(3) organization
MS Authorized MAR
looking for used laptops for developmentally disabled.
 
I tried:


'If responseContent = "<status>STATUS_CODE</status>" Then

Perhaps more data than that is coming back.
but the code doesn't capture the XML stream. What do I need please?

Take a look at the System.Text.XML classes - you should load the entire doc
into one of the XML class structures (XML Doc, XML reader, etc) and use
those classes to parse the response. You'll get a more accurate result than
merely text parsing.
 
Hello Michael,

As for the XML Response stream, how did you get it, through HttpWebRequest
compoent or any other means? Also, before you want to parse the XML
response content, where did you hold the XML content? In a string or a
inmemory stream?

In .net framework, most of the XML processing classes are under the
System.Xml namespace. And you can choose the proper class to maniplate XML
content depend on the format of data you hold. Here are some general
suggestions:

1. If the resposne XML content is held in a string, you can use the
XmlDocument class's "LoadXml" to load it.

#XmlDocument.LoadXml Method
http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx

2. If the response XML content is in an Stream object (memorystream,
HttpResponseStream....), you can use the XmlDocument.Load method to load it
into XmlDocument.

After you have load the XML content into XmlDocument instance, you can use
its "SelectSingleNode" or "SelectNodes" method to query specific nodes
(through XPATH string) from it and check the certain node's value.

#Select Nodes Using XPath Navigation
http://msdn2.microsoft.com/en-us/library/d271ytdx.aspx

#XML Document Object Model (DOM)
http://msdn2.microsoft.com/en-us/library/hf9hbf87.aspx

For more information about processing XML data in .net framework, you can
refer to the following MSDN reference:

#XML Documents and Data
http://msdn2.microsoft.com/en-us/library/2bcctyt8.aspx


Please feel free to let me know if you have any further specific questions
on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you for your response. The XML is streamed into a string I believe and
I can give you all the code that's used and that is being returned.


<plimus_licensing_response><status>ERROR_MAXCOUNT</status><days_since_last_assigned>1</days_since_last_assigned><days_till_expiration>358</days_till_expiration><use_count>1</use_count></plimus_licensing_response>

This is the string information that is returned from the license company.
The part that I need to parse and run logic on is the
<status>ERROR_CODE</status>

Based on the error_code, I need to build the IF Then Else statement to make
the code either open the program, return a message to the user, close the
program and refer the user to the support website.

Here is the code I'm using to return the values.

If Username.Text = "" And eMail.Text = "" And tbxLicense.Text = "" Then
MessageBox.Show("Please enter your registered name.")
Username.Focus()
ElseIf Username.Text <> "" And eMail.Text = "" And tbxLicense.Text =
"" Then
MessageBox.Show("Please enter the person's email for this
computer")
eMail.Focus()
ElseIf Username.Text <> "" And eMail.Text <> "" And tbxLicense.Text
= "" Then
MessageBox.Show("Please enter the license number:
###-####-####-####")
tbxLicense.Focus()
Else
Dim request As Net.HttpWebRequest
Dim response As Net.HttpWebResponse
Dim url As String =
"https://www.plimus.com/jsp/validateKey.jsp?action=REGISTER&productId=#####&key="
& tbxLicense.Text & "&action=REGISTER &uniqueMachineId&=" & GetOSProductKey()
& eMail.Text & Username.Text
request = Net.WebRequest.Create(url)
request.Method = "Get"
response = request.GetResponse()
Dim sr As IO.StreamReader = New
IO.StreamReader(response.GetResponseStream())
Dim responseContent As String = sr.ReadToEnd()

sr.Close()
response.Close()
Dim reader As Xml.XmlTextReader = Nothing
tbxResponseContent.Text = responseContent

I've looked at code samples and have started with the Dim reader as
Xml.XmlTextReader = Nothing

The XML response is currently returned to a textbox called
tbxResponseContent.Text. The XML stream is the responseContent.
--
Michael Bragg, President
eSolTec, Inc.
a 501(C)(3) organization
MS Authorized MAR
looking for used laptops for developmentally disabled.
 
Thanks for your reply Michael,

For the xml fragment you provide, you can simply use XPATH to query out the
<status> element and its innerTEXT data. For example, the following code
use xmlDocument to load the xml data from string, and use the
SelectSingleNode to query the node throug XPATH string:

=================================
Dim xmlstring =
"<plimus_licensing_response><status>ERROR_MAXCOUNT</status><days_since_last_
assigned>1</days_since_last_assigned><days_till_expiration>358</days_till_ex
piration><use_count>1</use_count></plimus_licensing_response>"

Dim doc As New System.Xml.XmlDocument

doc.LoadXml(xmlstring)

Dim node As XmlNode

node = doc.SelectSingleNode("/plimus_licensing_response/status")

If Not node Is Nothing Then
Response.Write("<br/>status: " & node.InnerText)
End If
===================================


BTW, the above xpath string only suit this particular case, for other xml
fragment, you need to calculate the proper xpath when you want to query
other node or nodelist.

Here are some articles introducing XPATH and xpath query in .net framework
XML components:

#XPath Tutorial
http://www.w3schools.com/xpath/

#XPath Selections and Custom Functions, and More
http://msdn.microsoft.com/msdnmag/issues/03/02/XMLFiles/

#.NET and XML: XPath Queries
http://www.developer.com/net/net/article.php/3383961

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top