WebService: lettura di dati da un servizio SOAP Zucchetti Evotre

' Il seguente snippet consente di importare dati da un web service Zucchetti Evotre.
' Come si può vedere dall'esempio di risposta XML, la struttura è un po' particolare in quanto l'XML con i dati è contenuto all'interno di un valore stringa di ritorno, ed è quello su cui deve essere fatto il parse.
' Struttura della risposta XML:
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
'   <soap:Body>
'   <evotre_anagrafica_QueryResponse xmlns="http://evotre_anagrafica.ws.localhost/">
'   <return><![CDATA[<?xml version="1.0"?>
'       <webRowSet xmlns="http://java.sun.com/xml/ns/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
'xsi:schemaLocation="http://java.sun.com/xml/ns/jdbc http://java.sun.com/xml/ns/jdbc/webrowset.xsd">
'        <properties>
'             --- PROPRIETA' GENERALI ---
'        </properties>
'        <metadata>
'          <column-count>12</column-count>
'          <column-definition>
'              --- PROPRIETA' DELLE COLONNE ---
'          </column-definition>
'        </metadata>
'        <data>
'          <currentRow>
'            --- DATI DEI SINGOLI CAMPI DELLE COLONNE ---
'            <columnValue></columnValue>
'          </currentRow>
'        </data>
'      </webRowSet>]]></return>
'   </evotre_anagrafica_QueryResponse>
'  </soap:Body>
'</soap:Envelope>

' Formazione SOAP Request e interrogazione WS
Dim xml As System.XML.Linq.XDocument = System.XML.Linq.XDocument.Parse("<?xml version=""1.0"" encoding=""utf-8""?>" +
                                    "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:evot=""http://evotre_anagrafica.ws.localhost/"">" +
                                    "  <soapenv:Header/> " +
                                    " <soapenv:Body> " +
                                    " <evot:evotre_anagrafica_Query> " +
                                    " <evot:m_UserName>ws_qualiware</evot:m_UserName>" +
                                    " <evot:m_Password>ws_qualiware</evot:m_Password> " +
                                    " <evot:m_Company>001</evot:m_Company> " +
                                    " <evot:m_bCount>0</evot:m_bCount> " +
                                    " </evot:evotre_anagrafica_Query> " +
                                    "</soapenv:Body> " +
                                    "</soapenv:Envelope>" )


Dim url As String = "https://yy.xxxxxxxxxxxxx.it:443/HRPORTAL/servlet/SQLDataProviderServer/evotre_anagrafica"
Dim webRequest__1 As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)

webRequest__1.Method = "POST"
webRequest__1.ContentType = "text/xml; charset=UTF-8"
webRequest__1.ContentLength = xml.ToString().Length
webRequest__1.Headers.Add("SOAPAction", """evotre_anagrafica_Query""")
Using requestWriter2 As New System.IO.StreamWriter(webRequest__1.GetRequestStream())
      requestWriter2.Write(xml.ToString())
End Using


' Recupero della risposta
Dim response As System.XML.Linq.XElement
Dim ok as boolean=true
Using resp As System.Net.HttpWebResponse = DirectCast(webRequest__1.GetResponse(), System.Net.HttpWebResponse)
     if resp.StatusCode = 200
        Using responseStream = resp.GetResponseStream()
              response = System.XML.Linq.XElement.Load(responseStream)
        End Using
     else
        dim msg as string = "Errore durante la connessione al Web Service." + vbcr + "Status: " + ltrim(str(resp.StatusCode)) + " " + resp.StatusDescription
        form.alert(msg)
        ok = false
     end if
End Using
		  
if not ok
   return
end if

' *** NECESSARIO ULTERIORE PARSING DELLA RISPOSTA - XML DENTRO XML ***
Dim response2 As System.XML.Linq.XElement = System.XML.Linq.XElement.Parse(response)


' Elaborazione della risposta e trasformazione in una DataTable
Dim t As System.data.DataTable = New System.data.DataTable
Dim c As System.data.DataColumn 
Dim row As System.data.DataRow
Dim i As Integer

' Predispongo le colonne della DataTable
Dim ns As System.XML.Linq.XNamespace = "http://java.sun.com/xml/ns/jdbc"
For Each el As System.XML.Linq.XElement In response2.Descendants(ns + "column-name")
	c = New System.data.DataColumn
	c.ColumnName = el.Value
	'form.alert(c.ColumnName)
	c.MaxLength = 100
	t.Columns.Add(c)
Next
		  
' Leggo i singolo 
For Each el As System.XML.Linq.XElement In response2.Descendants(ns + "currentRow")
	row = t.NewRow
	i = 0
	For Each f As System.XML.Linq.XElement In el.Nodes
		row(i) = f.Value
		i += 1
   Next
	t.Rows.Add(row)
Next

t.AcceptChanges()

' Trasformazione della DataTable in una QWTable
dim Q as new QWTable()
Q.RequestLive = False
Q.Table = t
Q.Rowset = New QWRowset(Q)

' Proposta all'utente dei records recuperati
form.ChooseRecords(form.findcontrol("RitornoChooseRecords"), Q, "Scegliere")