2

how do I parse the following xml data in XamarinForms:

<string xmlns="url">
{"UserName":"user1","Password":"pswd","Address1":"address"}
</string>

I am able to pasrse JSON data using JSON.Net component. But this particular response is wrapped up into XML. How shall I parse it?.

Thanks in advance.

Edit:

var request = new RestRequest (String.Format ("{0}/allinfo", "198440"));
                client.ExecuteAsync (request, response => {
                    System.Diagnostics.Debug.WriteLine("Response: "+response.Content);
                    pd.cancelDialog();
                    XDocument xd = XDocument.Load(response.Content);
                    var json = xd.Root.Element("string");
                    System.Diagnostics.Debug.WriteLine("Json Response: "+json);
});

I am able to see the "Response" but not "Json Response"

1 Answer 1

2
  1. First read xml using XLink & XDocument and get property which represents json
  2. Second parse json using JSON.NET

Here is a sample code:

XDocument xd = XDocument.Load(xmlStream);
String jsonResponse = xd.Root....
UserCredentials creds = JsonConvert.DeserializeObject<UserCredentials>(jsonResponse);

btw: it is not a good practice to return user credentials from the API response (and even store it at the backend).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply, but I am not able to parse it. I have edited my question with RestSharp code where I am parsing my response. Can u have a look and tell wats the problem.
okay, I have got it i shud inckude this in the above code string json = xd.Root.Value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.