Question Problem with Reading XML

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Hey everyone. I hope I can get some help here since I'm not really sure if I am doing this the right way.

I am sending a request that includes 4 domains which I would like to check the availability of. The response I receive (below) contains each domain with different responses that may be received. I am trying to figure out the best way to go through the XML and properly check for which ones come back as Available and which ones come back as Not Available.

VB.NET:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns7:SgndCommandResponse xmlns:ns7="http://www.nic.es/sgnd/schemas" xmlns:ns10="http://www.nic.es/sgnd/schemas-movement" xmlns:ns11="http://www.nic.es/sgnd/schemas-invoice" xmlns:ns12="http://www.nic.es/sgnd/schemas-tradeaction" xmlns:ns13="http://www.nic.es/sgnd/schemas-ownershipcertificate" xmlns:ns14="http://www.nic.es/sgnd/schemas-statistic" xmlns:ns2="http://www.nic.es/sgnd/schemas-domain" xmlns:ns3="http://www.nic.es/sgnd/schemas-order" xmlns:ns4="http://www.nic.es/sgnd/schemas-contact" xmlns:ns5="http://www.nic.es/sgnd/schemas-host" xmlns:ns6="http://www.nic.es/sgnd/schemas-dnssec" xmlns:ns8="http://www.nic.es/sgnd/schemas-inbox" xmlns:ns9="http://www.nic.es/sgnd/schemas-extraordinarycancelation">
         <ns7:DomainCreateResponse>
            <ns2:Name>domainavailable1.es</ns2:Name>
            <ns2:Name>domainavailable2.es</ns2:Name>
            <ns2:Autorenew>false</ns2:Autorenew>
            <ns2:Creation>2016-11-24T15:07:05.364+01:00</ns2:Creation>
            <ns2:Expiration>2017-11-24T15:07:05.364+01:00</ns2:Expiration>
            <ns2:Order>
               <ns3:OrderId>553905</ns3:OrderId>
               <ns3:OrderType>1</ns3:OrderType>
               <ns3:State>DMOK</ns3:State>
               <ns3:StateDescription>Efectuada</ns3:StateDescription>
               <ns3:Creation>2016-11-24T15:07:05.398+01:00</ns3:Creation>
               <ns3:Reference>AD-ES-553905-F5</ns3:Reference>
               <ns3:DomainData>
                  <ns2:Name>domainavailable1.es</ns2:Name>
               </ns3:DomainData>
            </ns2:Order>
            <ns2:Order>
               <ns3:OrderId>553906</ns3:OrderId>
               <ns3:OrderType>1</ns3:OrderType>
               <ns3:State>DMOK</ns3:State>
               <ns3:StateDescription>Efectuada</ns3:StateDescription>
               <ns3:Creation>2016-11-24T15:07:05.603+01:00</ns3:Creation>
               <ns3:Reference>AD-ES-553906-F5</ns3:Reference>
               <ns3:DomainData>
                  <ns2:Name>domainavailable2.es</ns2:Name>
               </ns3:DomainData>
            </ns2:Order>
            <ns2:NotIncluded>
               <ns2:ErrorCause id="3019">
                  <ns2:Name>notavailable.es</ns2:Name>
               </ns2:ErrorCause>
               <ns2:ErrorCause id="3001">
                  <ns2:Name>domain.incorrect</ns2:Name>
               </ns2:ErrorCause>
            </ns2:NotIncluded>
         </ns7:DomainCreateResponse>
         <ns7:ReturnCode>1000</ns7:ReturnCode>
         <ns7:ServerData>
            <ns7:Timestamp>2016-11-24T15:07:05.767+01:00</ns7:Timestamp>
            <ns7:ClientID>CLIENT-ID-AR-CHECKLIST</ns7:ClientID>
            <ns7:ServerID>1479996425767</ns7:ServerID>
         </ns7:ServerData>
      </ns7:SgndCommandResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The way I am currently trying to handle this is by looking for "ns2:Order" tags and then pulling the domain from each of those. As you can see above (domainavailable1.es) and (domainavailable2.es) are the two available domains here.

Next, I try to loop through and look for "ns2:NotIncluded" tags and pull the domain from there if it has an ErrorCause id of "3019".. As you can see above (notavailable.es) is the not available domain here.

Below is the complete code I am using to try and read through the XML above and pull the proper domain with the proper status (available) or (not available), but it does not seam to be working properly. Is there a better way I could be doing this?

VB.NET:
                        'READ RESPONSE WITH MULTIPLE DOMAINS
                        Dim doc As New XmlDocument()
                        doc.Load(WebResponse)
                        txtLog.AppendText(WebResponse & vbCrLf)

                        'HANDLE AVAILABLE DOMAINS
                        Dim nodelist As XmlNodeList = doc.GetElementsByTagName("ns2:Order")
                        For Each node As XmlElement In nodelist
                            Dim nodeDomain As String = String.Empty
                            nodeDomain = node("ns2:Name").InnerText
                            txtLog.AppendText(nodeDomain & " SUCCESS ...via ESNIC THREAD(1) at: " & TimeOfDay & vbCrLf)
                            removeDomain(nodeDomain)
                        Next

                        'HANDLE NOT AVAILABLE DOMAINS
                        Dim nodelist2 As Xml.XmlNodeList = doc.GetElementsByTagName("ns2:NotIncluded")
                        For Each cat As Xml.XmlElement In nodelist2
                            Dim nodeDomain2 As String = String.Empty
                            Dim sResult As String = cat.GetAttribute("id")
                            nodeDomain2 = cat("ns2:Name").Value
                            If sResult = "3019" Then
                                txtLog.AppendText(nodeDomain2 & " NOT AVAILABLE ...via ESNIC THREAD(1) at: " & TimeOfDay & vbCrLf)
                            Else
                                txtLog.AppendText(nodeDomain2 & " UNKNOWN ERROR (ID: " & sResult & ") ...via ESNIC THREAD(1) at: " & TimeOfDay & vbCrLf)
                            End If
                        Next

Thanks in advance!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
I would rather use the newer (year 2008) xml tools:
Imports <xmlns:ns2="http://www.nic.es/sgnd/schemas-domain">

Dim doc = XDocument.Load(WebResponse)
Dim available = From name In doc...<ns2:Order>...<ns2:Name> Select name.Value

Dim unavailable = From name In doc...<ns2:NotIncluded>...<ns2:Name> Where name.Parent.@id = "3019" Select name.Value
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
I would rather use the newer (year 2008) xml tools:
Imports <xmlns:ns2="http://www.nic.es/sgnd/schemas-domain">

Dim doc = XDocument.Load(WebResponse)
Dim available = From name In doc...<ns2:Order>...<ns2:Name> Select name.Value

Dim unavailable = From name In doc...<ns2:NotIncluded>...<ns2:Name> Where name.Parent.@id = "3019" Select name.Value

Thanks for your help and example code JohnH! I will try using XDocument when I get back to my computer and see if I can get it working this way instead. Will update the thread tomorrow once I can play with it some more tomorrow. Thanks again!
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
@JohnH - I was able to implement your suggestion and give it a try. However, I only receive "Invalid characters in the path" responses. The issue appears to be happening here:
VB.NET:
Dim doc = XDocument.Load(WebResponse)

Here is a screenshot of my WebResponse in the example:
es.png

Could this be happening because there are no "available" domains in this test?

Thanks again for your help!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
XmlDocument.Load and XDocument.Load methods expects same type of parameters for source of xml. It could be a String path, or a Stream for example. I saw you used Load(WebResponse) and thought it was a Stream. If WebResponse is String contents of xml then you have to use XmlDocument.LoadXml method, and in my example XDocument.Parse method.
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Thanks for your response JohnH! That's my mistake for not including any of the call/response in my initial code. I already got the stream and converted the response to a string when I reached this point. I changed things around a bit and now I actually get some different errors.

First, I tried just this
VB.NET:
                        Dim doc As XDocument = XDocument.Parse(WebResponse)

Which returned an error: The 'Prompt' argument can not be converted to type 'String'

I then tried this
VB.NET:
                        Dim doc As XDocument = XDocument.Parse(WebResponse.ToStrng)

Which gave an error: Root level data is invalid. Line 1, position 1

I then tried both LoadXml and XDocument.Parse, with adding <root> in there:
VB.NET:
                        Dim testDoc As XmlDocument = New XmlDocument()
                        testDoc.LoadXml(WebResponse)
                        Dim doc As XDocument = XDocument.Parse(<root><%= testDoc.ToString %></root>)
                        MsgBox(testDoc.ToString)

However, I still get the error: Root level data is invalid. Line 1, position 1.

I'll do some more testing with and without the <root></root> tags being manually added, but I'm not really sure why it's giving me that Root error even once I've added that..
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Hey JohnH - Thanks for your reply! After further looking it appears the String error I was having had to do with a messagebox I had in there. This is working now exactly as you described. Thanks again!!
 
Top Bottom