Ismail Khan
Ismail Khan

Reputation: 93

Parse XML in Xamarin Forms

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"

Upvotes: 2

Views: 2587

Answers (1)

Mando
Mando

Reputation: 11712

  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).

Upvotes: 2

Related Questions