C# Error: RestClient read value from SoapENV XML

114 Views Asked by At

I am try to get session ID from soapenv using RestClient but i get value as 0 or -1.

This is my code

var client = new RestClient(url);
        client.Authenticator = new HttpBasicAuthenticator(name, pwd);
        var request = new RestRequest("/{sessionID}",Method.GET);

        var response = client.Get(request);
        var content = response.Content; // Raw content as string
        Console.WriteLine(response.ContentLength);

My Soapenv looks like this

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v7="urn://oracle.bi.webservices/v7">
   <soapenv:Header/>
   <soapenv:Body>
      <v7:logon>
         <v7:name>pxxxx</v7:name>
         <v7:password>Pxxxx</v7:password>
      </v7:logon>
   </soapenv:Body>
</soapenv:Envelope>

I am trying to get Session ID (9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l) that looks likes this

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:sawsoap="urn://oracle.bi.webservices/v7">
   <soap:Body>
      <sawsoap:logonResult>
         <sawsoap:sessionID xsi:type="xsd:string">9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l</sawsoap:sessionID>
      </sawsoap:logonResult>
   </soap:Body>
</soap:Envelope>

Not able to figure out the how to get the session ID. Can anyone help?

1

There are 1 best solutions below

2
On

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApp1
{
    class Program
    {
        const string FILENAME = @"c:\Temp\Test.xml";
        static void Main(string[] args)
        {
            string content = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(content);
            XElement xSessionID = doc.Descendants().Where(x => x.Name.LocalName == "sessionID").FirstOrDefault();
            string sessionID = (string)xSessionID;

        }
    }
}