SOAP Request External Web Service

openutopenut Member Posts: 5
edited 2015-12-21 in NAV Three Tier
Hello everyone, I trying to put into a message the response of a soap request.

I know i get the answer because of the xml archive saved, now I need to acces to that info, any ideas?

This is mi codeunit that send a request an recieve the response:

OBJECT Codeunit 50050 Request XML
{
OBJECT-PROPERTIES
{
Date=18/12/15;
Time=10:33:18 a.m.;
Modified=Yes;
Version List=;
}
PROPERTIES
{
OnRun=VAR
xml@1000000000 : Text[250];
url@1000000001 : Text[250];
soapActionUrl@1000000002 : Text[250];
BEGIN

xml := '<GetCitiesByCountry xmlns="http://www.webserviceX.NET"><CountryName>Guyana</CountryName></GetCitiesByCountry>';
url := 'http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry';
soapActionUrl := '"http://www.webserviceX.NET/GetCitiesByCountry"';

sb := sb.StringBuilder();
sb.Append('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>');
sb.Append(xml);
sb.Append('</soap:Body></soap:Envelope>');
uriObj := uriObj.Uri(url);
lgRequest := lgRequest.CreateDefault(uriObj);
lgRequest.Method := 'POST';
lgRequest.ContentType := 'text/xml';
lgRequest.Headers.Add('SOAPAction', soapActionUrl);
lgRequest.Credentials := credentials.DefaultCredentials;
lgRequest.Timeout := 120000;
stream := stream.StreamWriter(lgRequest.GetRequestStream(), ascii.UTF8);
stream.Write(sb.ToString());
stream.Close();
lgResponse := lgRequest.GetResponse();
str := lgResponse.GetResponseStream();
reader := reader.XmlTextReader(str);
document := document.XmlDocument();
document.Load(reader);
document.Save('C:\Temp\document.xml');
//xmlnodelist := document.GetElementsByTagName('City');
xmlnodelist := document.SelectNodes('City');
xmlnode := xmlnodelist.Item(0);
MESSAGE(xmlnode.Value());
reader.Close();
str.Close();
END;

}
CODE
{
VAR
sb@1000000000 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.StringBuilder";
uriObj@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri";
lgRequest@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";
stream@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter";
lgResponse@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";
str@1000000005 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
reader@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader";
document@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
ascii@1000000008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";
credentials@1000000009 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.CredentialCache";
xmlnodelist@1000000010 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList";
xmlnode@1000000011 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";

EVENT document@1000000007::NodeInserting@93(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

EVENT document@1000000007::NodeInserted@94(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

EVENT document@1000000007::NodeRemoving@95(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

EVENT document@1000000007::NodeRemoved@96(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

EVENT document@1000000007::NodeChanging@97(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

EVENT document@1000000007::NodeChanged@98(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
BEGIN
END;

BEGIN
END.
}
}

This is the file generated:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<soap:Body>
<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET"&gt;
<GetCitiesByCountryResult><NewDataSet>
<Table>
<Country>Guyana</Country>
<City>Georgetown</City>
</Table>
<Table>
<Country>Guyana</Country>
<City>Timehri Airport</City>
</Table>
</NewDataSet></GetCitiesByCountryResult>
</GetCitiesByCountryResponse>
</soap:Body>
</soap:Envelope>

EDIT

I have improved my code with this lines:


document.Save('C:\Temp\document.xml');

xmlnodelist := document.SelectNodes('/');
xmlnode := xmlnodelist.Item(0);
MESSAGE(xmlnode.FirstChild.InnerText);
MESSAGE(xmlnode.LastChild.InnerText);

reader.Close();

I still cannot get the name of a city, this are the messages displayed:

<img src="https://us.v-cdn.net/5022383/uploads/FileUpload/52/eb6743f6ee79586b88d54246f0bce0.png&quot; />

<img src="https://us.v-cdn.net/5022383/uploads/FileUpload/ca/884e34b50170a5468ca41fd02434b8.png&quot; />


EDIT

Seems like the code was OK, the response is only one string, to get city by city will be a job for other time.

Best Answer

  • openutopenut Member Posts: 5
    Answer ✓
    I leave you the code using CelsiusToFahrenheit web service.
    There may be more variables than the needed.

    OBJECT Codeunit 50060 SOAP Grados
    {
    OBJECT-PROPERTIES
    {
    Date=21/12/15;
    Time=02:19:48 p.m.;
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    OnRun=VAR
    xml@1000000000 : Text[250];
    url@1000000001 : Text[250];
    soapActionUrl@1000000002 : Text[250];
    i@1000000003 : Integer;
    j@1000000004 : Integer;
    BEGIN

    xml := '<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"><Celsius>19</Celsius></CelsiusToFahrenheit>';
    url := 'http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit';
    soapActionUrl := '"http://www.w3schools.com/webservices/CelsiusToFahrenheit"';

    sb := sb.StringBuilder();
    sb.Append('<?xml version="1.0" encoding="utf-8"?><soap:Envelope ' +
    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>');
    sb.Append(xml);
    sb.Append('</soap:Body></soap:Envelope>');
    uriObj := uriObj.Uri(url);
    lgRequest := lgRequest.CreateDefault(uriObj);
    lgRequest.Method := 'POST';
    lgRequest.ContentType := 'text/xml; charset=utf-8';
    lgRequest.Headers.Add('SOAPAction', soapActionUrl);
    stream := stream.StreamWriter(lgRequest.GetRequestStream(), ascii.UTF8);
    stream.Write(sb.ToString());
    stream.Close();
    lgResponse := lgRequest.GetResponse();
    str := lgResponse.GetResponseStream();
    reader := reader.XmlTextReader(str);
    document := document.XmlDocument();
    document.Load(reader);
    document.Save('C:\Temp\document.xml');
    xmlnodelist := document.SelectNodes('/');
    i := xmlnodelist.Count;
    xmlnode := xmlnodelist.Item(0);
    MESSAGE(xmlnode.LastChild.InnerText);
    reader.Close();
    str.Close();
    END;

    }
    CODE
    {
    VAR
    sb@1000000000 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.StringBuilder";
    uriObj@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri";
    lgRequest@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";
    stream@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter";
    lgResponse@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";
    str@1000000005 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
    reader@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader";
    document@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
    ascii@1000000008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";
    credentials@1000000009 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.CredentialCache";
    xmlnodelist@1000000010 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList";
    xmlnode@1000000011 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";
    xmlelement@1000000012 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlElement";

    EVENT document@1000000007::NodeInserting@93(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeInserted@94(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeRemoving@95(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeRemoved@96(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeChanging@97(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeChanged@98(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    BEGIN
    END.
    }
    }

Answers

  • peterz84peterz84 Member Posts: 8
    Try XmlNodeList.Item(0).InnerText
  • openutopenut Member Posts: 5
    Thank you, but it doesn't work, I keep trying
  • openutopenut Member Posts: 5
    Answer ✓
    I leave you the code using CelsiusToFahrenheit web service.
    There may be more variables than the needed.

    OBJECT Codeunit 50060 SOAP Grados
    {
    OBJECT-PROPERTIES
    {
    Date=21/12/15;
    Time=02:19:48 p.m.;
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    OnRun=VAR
    xml@1000000000 : Text[250];
    url@1000000001 : Text[250];
    soapActionUrl@1000000002 : Text[250];
    i@1000000003 : Integer;
    j@1000000004 : Integer;
    BEGIN

    xml := '<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"><Celsius>19</Celsius></CelsiusToFahrenheit>';
    url := 'http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit';
    soapActionUrl := '"http://www.w3schools.com/webservices/CelsiusToFahrenheit"';

    sb := sb.StringBuilder();
    sb.Append('<?xml version="1.0" encoding="utf-8"?><soap:Envelope ' +
    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>');
    sb.Append(xml);
    sb.Append('</soap:Body></soap:Envelope>');
    uriObj := uriObj.Uri(url);
    lgRequest := lgRequest.CreateDefault(uriObj);
    lgRequest.Method := 'POST';
    lgRequest.ContentType := 'text/xml; charset=utf-8';
    lgRequest.Headers.Add('SOAPAction', soapActionUrl);
    stream := stream.StreamWriter(lgRequest.GetRequestStream(), ascii.UTF8);
    stream.Write(sb.ToString());
    stream.Close();
    lgResponse := lgRequest.GetResponse();
    str := lgResponse.GetResponseStream();
    reader := reader.XmlTextReader(str);
    document := document.XmlDocument();
    document.Load(reader);
    document.Save('C:\Temp\document.xml');
    xmlnodelist := document.SelectNodes('/');
    i := xmlnodelist.Count;
    xmlnode := xmlnodelist.Item(0);
    MESSAGE(xmlnode.LastChild.InnerText);
    reader.Close();
    str.Close();
    END;

    }
    CODE
    {
    VAR
    sb@1000000000 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.StringBuilder";
    uriObj@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Uri";
    lgRequest@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";
    stream@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamWriter";
    lgResponse@1000000004 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";
    str@1000000005 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
    reader@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlTextReader";
    document@1000000007 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";
    ascii@1000000008 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";
    credentials@1000000009 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.CredentialCache";
    xmlnodelist@1000000010 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList";
    xmlnode@1000000011 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";
    xmlelement@1000000012 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlElement";

    EVENT document@1000000007::NodeInserting@93(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeInserted@94(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeRemoving@95(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeRemoved@96(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeChanging@97(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    EVENT document@1000000007::NodeChanged@98(sender@1000000001 : Variant;e@1000000000 : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeChangedEventArgs");
    BEGIN
    END;

    BEGIN
    END.
    }
    }
  • Remco_ReinkingRemco_Reinking Member Posts: 74
    Try to find how to read XMLdocuments using dotnet or automation. I think there has been written a lot about this already. An example in NAV2016 is to be found in the function LogVerification in codeunit 249.
Sign In or Register to comment.