Subha Subha - 2 months ago 19
C# Question

Forming XML from Json String Xamarin Android

I am developing in Xamarin.Android. Trying to download xml from url as below and converting it to list of objects. But I am not able to form xml.

WebClient client = new WebClient();
//client.Headers.Add("Accept-Language", " en-US");
//client.Headers.Add("Accept", "application/xml");
//client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
string downloadString = client.DownloadString(url);
XDocument xml = XDocument.Parse(downloadString);

//XmlDocument doc = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(downloadString);


In above code different things are happening:


  1. If I add headers, then no exception was thrown while parsing, but if I comment it, then while parsing I am getting exception as




Data at the root level is invalid. Line 1, position 1.



  1. After including Headers, I tried to convert it to valid xml by deserializing the json string, then below compile error was shown




Error 2 'Newtonsoft.Json.JsonConvert' does not contain a definition
for 'DeserializeXmlNode'


I have added required namespaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Newtonsoft.Json;


It is not PCL project and its not using any specific profile also. Please someone help me.

I could form xml by simply doing this:

XDocument xml = XDocument.Parse(downloadString);


But after that I am not able to form list of object from xml.

var query = from data in doc.Root.Elements("Customer")
select new Customer
{
City = (string)data.Element("Address").Value,
CompanyName = (string)data.Element("Name").Value,
ID = Convert.ToInt32(data.Element("ID").Value),
WebsiteAddress = (string)data.Element("Website").Value
};

var list = query.ToList();


Even though I have valid xml with 20 datas above query always returns 0 count.

Json:


[{"ID":1,"Name":"Tata Consultancy
Services","Website":"www.tcs.com","Address":"Chennai","ImagePath":"http://test/lab/logos/tcs.png"},{"ID":2,"Name":"Tech
Mahindra","Website":"www.techmahindra.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/tmah.png"},{"ID":3,"Name":"Accenture
Services Pvt.
Ltd","Website":"www.accenture.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/accenture.png"},{"ID":4,"Name":"American
Express
(India)","Website":"www.americanexpress.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/amex.png"},{"ID":5,"Name":"California
Software Co.
Ltd","Website":"www.calsoftgroup.com","Address":"Chennai","ImagePath":"http://test/lab/logos/calsoft.png"},{"ID":6,"Name":"Datamatics
Ltd","Website":"www.datamatics.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/datamat.png"},{"ID":7,"Name":"Canon
India Pvt Ltd","Website":"www.canon-asia.com","Address":"New
Delhi","ImagePath":"http://test/lab/logos/canon.png"},{"ID":8,"Name":"Symantec
Ltd","Website":"www.symsntec.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/syman.png"},{"ID":9,"Name":"Cisco
Systems (India) Pvt. Ltd","Website":"www.cisco.com","Address":"New
Delhi","ImagePath":"http://test/lab/logos/cisco.png"},{"ID":10,"Name":"Elgi
Software
Ltd","Website":"www.lg.com/india","Address":"Mumbai","ImagePath":"http://test/lab/logos/lg.png"},{"ID":11,"Name":"FLSmidth
Private
Limited","Website":"www.flsmidth.com","Address":"Chennai","ImagePath":"http://test/lab/logos/fls.png"},{"ID":12,"Name":"Genesis
Infotech Solutions Pvt.
Ltd","Website":"www.genesisintl.com","Address":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":13,"Name":"Godrej
Infotech
Ltd","Website":"www.godrej.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/godrej.png"},{"ID":14,"Name":"Hamilton
Research & Technology Pvt.
Ltd","Website":"www.hamiltonresearch.com","Address":"Calcutta","ImagePath":"http://test/lab/logos/"},{"ID":15,"Name":"HCL
Technologies
Ltd","Website":"","Address":"","ImagePath":"http://test/lab/logos/hcl.png"},{"ID":16,"Name":"Hexaware
Technologies
Limited","Website":"www.hexaware.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/hex.png"},{"ID":17,"Name":"HTC
Software Development
Centre","Website":"","Address":"chennai","ImagePath":"http://test/lab/logos/htc.png"},{"ID":18,"Name":"Huawei
Technologies India Pvt
Ltd","Website":"","Address":"Bangalore","ImagePath":"http://test/lab/logos/huawei.png"},{"ID":19,"Name":"ICICI
Infotech Services
Ltd","Website":"","Address":"Mumbai","ImagePath":"http://test/lab/logos/"},{"ID":20,"Name":"Infosys
Technologies
Ltd","Website":"www.infy.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":21,"Name":"GE
Capital Services
India","Website":"www.ge.com","Address":"Gurgaon","ImagePath":"http://test/lab/logos/"},{"ID":22,"Name":"Godrej
Infotech
Ltd","Website":"www.godrej.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/"},{"ID":23,"Name":"Kirloskar
Computer Services
Ltd","Website":"www.kcsl.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":24,"Name":"Larsen
& Toubro Infotech
Limite","Website":"www.lntinfotech.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/"},{"ID":25,"Name":"Motorola
India Electronics Private
Ltd","Website":"","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":26,"Name":"Newgen
Software Technologies Ltd","Website":"","Address":"New
Delhi","ImagePath":"http://test/lab/logos/"},{"ID":27,"Name":"NIIT
Ltd","Website":"www.niit.com","Address":"New
Delhi","ImagePath":"http://test/lab/logos/niit.png"},{"ID":28,"Name":"Novell
Software Development (I)
Ltd","Website":"www.novell.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/novell.png"},{"ID":29,"Name":"Oracle
Solution Services (India) Pvt
Ltd","Website":"www.oracle.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":30,"Name":"Ramco
Systems
Ltd","Website":"www.ramco.com","Address":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":31,"Name":"Sterling
Infotech
Limited","Website":"www.sterlingcarnegie.com","Address":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":32,"Name":"SRM
Technologies
Limited","Website":"www.srmsoft.com","Address":"chennai","ImagePath":"http://test/lab/logos/srm.jpg"},{"ID":33,"Name":"SAP
India Pvt.
Ltd","Website":"www.sap.com/india","Address":"Bangalore","ImagePath":"http://test/lab/logos/sap.png"}]

Answer

First create a class like this below.

[Serializable]
public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Website { get; set; }
    public string Address { get; set; }
    public string ImagePath { get; set; }
}

Then you should be able to serialize/deserialize it the way you want.

    // You are getting the json from the web client. I am just using hard coded json to demonstrate.
    var json = @"[{""ID"":1,""Name"":""Tata Consultancy Services"",""Website"":""www.tcs.com"",""Address"":""Chennai"",""ImagePath"":""http://test/lab/logos/tcs.png""},{""ID"":2,""Name"":""Tech Mahindra"",""Website"":""www.techmahindra.com"",""Address"":""Mumbai"",""ImagePath"":""http://test/lab/logos/tmah.png""},{""ID"":3,""Name"":""Accenture Services Pvt. Ltd"",""Website"":""www.accenture.com"",""Address"":""Mumbai"",""ImagePath"":""http://test/lab/logos/accenture.png""},{""ID"":4,""Name"":""American Express (India)"",""Website"":""www.americanexpress.com"",""Address"":""Mumbai"",""ImagePath"":""http://test/lab/logos/amex.png""}]";

    //this will give you a list of customers.
    var customers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Customer>>(json);

    //To convert the list of customers to xml, do this.
    XmlSerializer xmlSerializer = new XmlSerializer(customers.GetType());

    using (StringWriter textWriter = new StringWriter())
    {
        xmlSerializer.Serialize(textWriter, customers);
        var xml = textWriter.ToString(); // xml variable should have what you are looking for.
    }

You can get a list of customers from "customers" variable.

enter image description here