从Json String Xamarin Android

时间:2016-09-12 11:30:33

标签: c# json xml xamarin.android

我正在开发Xamarin.Android。尝试从url下载xml,如下所示,并将其转换为对象列表。但是我无法形成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);

在上面的代码中,发生了不同的事情:

  1. 如果我添加标题,那么在解析时不会抛出任何异常,但如果我发表评论,那么在解析时我会遇到异常
  2.   

    根级别的数据无效。第1行,第1位。

    1. 包含Headers后,我尝试通过反序列化json字符串将其转换为有效的xml,然后显示下面的编译错误
    2.   

      错误2' Newtonsoft.Json.JsonConvert'不包含定义   for' DeserializeXmlNode'

      我添加了必需的命名空间。

      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;
      

      这不是PCL项目,也没有使用任何特定的配置文件。请有人帮助我。

      我可以通过简单地执行此操作来形成xml:

      XDocument xml = XDocument.Parse(downloadString);
      

      但之后我无法从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();
      

      即使我有上面有20个数据的有效xml查询总是返回0计数。

      JSON:

        

      [{" ID":1," Name":" Tata Consultancy   服务""网址":" www.tcs.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/tcs.png"},{" ID":2"名称":"技术   马辛德拉""网址":" www.techmahindra.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/tmah.png"},{" ID":3,"名称":"埃森哲   服务列兵。   公司""网址":" www.accenture.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/accenture.png"},{" ID&#34:4,"名称":"美国   表达   (印度)""网址":" www.americanexpress.com""地址":"孟买"&# 34;的ImagePath":" http://test/lab/logos/amex.png"},{" ID":5,"名称":"加州   软件公司   公司""网址":" www.calsoftgroup.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/calsoft.png"},{" ID":6,"名称":" Datamatics   公司""网址":" www.datamatics.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/datamat.png"},{" ID":7,"名称":"佳能   印度私人有限公司","网站":" www.canon-asia.com","地址":"新   德里""的ImagePath":" http://test/lab/logos/canon.png"},{" ID":8,"名称&#34 ;: "赛门铁克   公司""网址":" www.symsntec.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/syman.png"},{" ID":9,"名称":"思科   系统(印度)列兵。公司""网站":" www.cisco.com""地址":"新   德里""的ImagePath":" http://test/lab/logos/cisco.png"},{" ID":10,"名称&#34 ;: " ELGI   软件   公司""网站":" www.lg.com/india","地址":"孟买"&#34 ;的ImagePath":" http://test/lab/logos/lg.png"},{" ID":11,"名称":" FLSmidth公司   私人的   限量""网址":" www.flsmidth.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/fls.png"},{" ID":12,"名称":"创   Infotech Solutions Pvt。   公司""网址":" www.genesisintl.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":13,"名称":"戈德瑞   信息技术   公司""网址":" www.godrej.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/godrej.png"},{" ID":14,"名称":"汉密尔顿   研究与发展技术列兵   公司""网址":" www.hamiltonresearch.com""地址":"加尔各答""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":15,"名称":" HCL   技术   公司""网址":"""地址":"""的ImagePath&#34 ;: " http://test/lab/logos/hcl.png"},{" ID":16,"名称":" Hexaware   技术   限量""网址":" www.hexaware.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/hex.png"},{" ID":17,"名称":" HTC   软件开发   中心""网址":"""地址":"奈""的ImagePath&#34 ;: " http://test/lab/logos/htc.png"},{" ID":18,"名称":"华为   Technologies India Pvt   公司""网址":"""地址":"班加罗尔""的ImagePath&#34 ;: " http://test/lab/logos/huawei.png"},{" ID":19,"名称":" ICICI   信息技术服务   公司""网址":"""地址":"孟买""的ImagePath&#34 ;: " http://test/lab/logos/"},{" ID":20,"名称":" Infosys的   技术   公司""网址":" www.infy.com""地址":"班加罗尔""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":21,"名称":" GE   资本服务   印度""网址":" www.ge.com""地址":"古尔冈""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":22,"名称":"戈德瑞   信息技术   公司""网址":" www.godrej.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":23,"名称":"基洛斯卡   电脑服务   公司""网址":" www.kcsl.com""地址":"班加罗尔""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":24,"名称":"拉森   &安培; Toubro Infotech   LIMITE""网址":" www.lntinfotech.com""地址":"孟买""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":25,"名称":"摩托罗拉   印度电子私人   公司""网址":"""地址":"班加罗尔""的ImagePath&#34 ;: " http://test/lab/logos/"},{" ID":26,"名称":" NEWGEN   软件技术有限公司","网站":"","地址":"新   德里""的ImagePath":" http://test/lab/logos/"},{" ID":27,"名称&#34 ;: " NIIT   公司""网站":" www.niit.com""地址":"新   德里""的ImagePath":" http://test/lab/logos/niit.png"},{" ID":28,"名称&#34 ;: " Novell公司   软件开发(I)   公司""网址":" www.novell.com""地址":"班加罗尔""的ImagePath& #34;:" http://test/lab/logos/novell.png"},{" ID":29,"名称":"甲骨文   解决方案服务(印度)列兵   公司""网址":" www.oracle.com""地址":"班加罗尔""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":30,"名称":"的Ramco   系统   公司""网址":" www.ramco.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":31,"名称":"斯特林   信息技术   限量""网址":" www.sterlingcarnegie.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/"},{" ID":32,"名称":" SRM   技术   限量""网址":" www.srmsoft.com""地址":"奈""的ImagePath& #34;:" http://test/lab/logos/srm.jpg"},{" ID":33,"名称":" SAP   印度列兵   公司""网站":" www.sap.com/india","地址":"班加罗尔"&#34 ;的ImagePath":" http://test/lab/logos/sap.png"}]

1 个答案:

答案 0 :(得分:1)

首先在下面创建一个类。

[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; }
}

然后你应该能够按照你想要的方式序列化/反序列化它。

    // 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.
    }

您可以从“customers”变量中获取客户列表。

enter image description here