Ejemplo n.º 1
0
        public static ApiResponse ParseXmlResponse(Stream stream)
        {
            using (var xmlReader = XmlReader.Create(stream))
            {
                var apiResponse = new ApiResponse();

                xmlReader.ReadToNextElement(); // Initial read
                xmlReader.ReadToNextElement(); // Move to first descendant

                while (!xmlReader.EOF)
                {
                    if (xmlReader.NodeType == XmlNodeType.Element)
                    {
                        switch (xmlReader.Name)
                        {
                            case "statusCode":
                                apiResponse.StatusCode = xmlReader.ReadElementContentAsInt();
                                break;

                            case "count":
                                apiResponse.Count = xmlReader.ReadElementContentAsInt();
                                break;

                            case "response":
                                apiResponse.Response = xmlReader.ReadOuterXml();
                                break;

                            case "error":
                                throw ParseError(xmlReader);

                            default:
                                xmlReader.Skip();
                                break;
                        }
                    }
                    else
                    {
                        xmlReader.Read();
                    }
                }

                return apiResponse;
            }
        }
Ejemplo n.º 2
0
        public static ApiResponse ParseJsonResponse(Stream stream)
        {
            using (var jsonReader = new JsonTextReader(new StreamReader(stream)))
            {
                var apiResponse = new ApiResponse();

                while (jsonReader.Read())
                {
                    if (jsonReader.TokenType == JsonToken.PropertyName)
                    {
                        switch ((string)jsonReader.Value)
                        {
                            case "statusCode":
                                apiResponse.StatusCode = jsonReader.ReadAsInt32() ?? 0;
                                break;

                            case "count":
                                apiResponse.Count = jsonReader.ReadAsInt32() ?? 0;
                                break;

                            case "response":
                                apiResponse.Response = jsonReader.GetInnerJson();
                                break;

                            case "error":
                                jsonReader.Read();
                                throw ParseError(jsonReader);

                            default:
                                jsonReader.Skip();
                                break;
                        }
                    }
                }

                return apiResponse;
            }
        }