0

My xamarin mobile app consumes soap service, when i make a login request the response returned has both Json and xml. I am interested only in the json string. Can any one tell me the way to parse the following response.

[{"Result":"true","HasError":false,"UserMsg":null,"ErrorMsg":null,"TransporterID":"327f6da2-d797-e311-8a6f-005056a34fa8"}] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><LoginResponse xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

karthick
  • 351
  • 1
  • 5
  • 18

2 Answers2

0

String.IndexOf will give you the index of the xml part, then you can use String.Substring:

string str = @"[{ ""Result"":""true"",""HasError"":false,""UserMsg"":null,""ErrorMsg"":null,""TransporterID"":""327f6da2-d797-e311-8a6f-005056a34fa8""}]
<? xml version = ""1.0"" encoding = ""utf-8"" ?>< soap : Envelope xmlns: soap = ""http://www.w3.org/2003/05/soap-envelope"" xmlns: xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns: xsd = ""http://www.w3.org/2001/XMLSchema"" >< soap:Body >< LoginResponse xmlns = ""http://tempuri.org/"" /></ soap:Body ></ soap:Envelope >";

string json = str.Substring(0, str.IndexOf("<? xml")); 

Console.WriteLine(json); // [{ "Result":"true","HasError":false,"UserMsg":D":"327f6da2-d797-e311-8a6f-005056a34fa8"}]
w.b
  • 11,026
  • 5
  • 30
  • 49
0

You can use the Substring method as follows:

string response = "<<The response you got>>";
string jsonResponse = response.Substring(0, response.IndexOf("<?"));

The 0 is the starting index (when to start extracting the substring) and the IndexOf will return the index of <? which is the start of the XML part of the response. You can read about the Substring Method here.

Therefore, you have filtered the JSON response from the whole string. (For a how-to parse JSON data, check this answer).

Community
  • 1
  • 1
Ziad Akiki
  • 2,601
  • 2
  • 26
  • 41