2015-07-19 57 views
0

我的xamarin移動應用程序使用soap服務,當我發出登錄請求時,返回的響應包含Json和xml。我只對json字符串感興趣。任何人都可以告訴我解析以下回答的方法嗎?C#從字符串中去掉xml

[{"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>

回答

0

String.IndexOf會給你的XML部分的指數,那麼你可以使用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"}] 
0

可以使用Substring方法如下:

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

0是起始索引(何時開始提取子字符串)和IndexOf將返回索引<?,這是響應的XML部分的開始。您可以閱讀有關Substring方法here

因此,您已經過濾了整個字符串中的JSON響應。 (關於如何解析JSON數據,請檢查this answer)。