2015-07-19 136 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)。