【发布时间】:2016-09-12 11:30:33
【问题描述】:
我正在 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 行,位置 1。
- 包含Headers后,我尝试通过反序列化json字符串将其转换为有效的xml,然后显示下面的编译错误
错误 2 'Newtonsoft.Json.JsonConvert' 不包含定义 对于'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();
即使我有有效的 xml,上面有 20 个数据,查询总是返回 0 个计数。
json:
[{"ID":1,"Name":"塔塔咨询公司 服务","网站":"www.tcs.com","地址":"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 服务列兵。 Ltd","Website":"www.accenture.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/accenture.png"},{"ID":4,"Name":"American 表示 (印度)","Website":"www.americanexpress.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/amex.png"},{"ID":5,"Name":"California 软件公司 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","网站":"www.canon-asia.com","地址":"新 德里","ImagePath":"http://test/lab/logos/canon.png"},{"ID":8,"Name":"赛门铁克 Ltd","Website":"www.symsntec.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/syman.png"},{"ID":9,"Name":"Cisco 系统(印度)列兵。有限公司","网站":"www.cisco.com","地址":"新 德里","ImagePath":"http://test/lab/logos/cisco.png"},{"ID":10,"Name":"Elgi 软件 Ltd","Website":"www.lg.com/india","Address":"Mumbai","ImagePath":"http://test/lab/logos/lg.png"},{"ID":11,"Name":"FLSmidth 私人的 Limited","Website":"www.flsmidth.com","Address":"Chennai","ImagePath":"http://test/lab/logos/fls.png"},{"ID":12,"Name":"Genesis 信息技术解决方案列兵。 Ltd","网站":"www.genesisintl.com","地址":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":13,"Name":"Godrej 信息技术 Ltd","Website":"www.godrej.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/godrej.png"},{"ID":14,"Name":"Hamilton 研究与技术列兵。 Ltd","网站":"www.hamiltonresearch.com","地址":"Calcutta","ImagePath":"http://test/lab/logos/"},{"ID":15,"Name":"HCL 技术 有限公司","网站":"","地址":"","ImagePath":"http://test/lab/logos/hcl.png"},{"ID":16,"Name":"Hexaware 技术 Limited","Website":"www.hexaware.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/hex.png"},{"ID":17,"Name":"HTC 软件开发 中心","网站":"","地址":"chennai","ImagePath":"http://test/lab/logos/htc.png"},{"ID":18,"名称":"华为 印度技术私人有限公司 有限公司","网站":"","地址":"班加罗尔","ImagePath":"http://test/lab/logos/huawei.png"},{"ID":19,"名称":"ICICI 信息技术服务 有限公司","网站":"","地址":"孟买","ImagePath":"http://test/lab/logos/"},{"ID":20,"名称":"Infosys 技术 Ltd","Website":"www.infy.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":21,"Name":"GE 资本服务 印度","网站":"www.ge.com","地址":"Gurgaon","ImagePath":"http://test/lab/logos/"},{"ID":22,"Name":"Godrej 信息技术 Ltd","网站":"www.godrej.com","地址":"Mumbai","ImagePath":"http://test/lab/logos/"},{"ID":23,"Name":"Kirloskar 计算机服务 Ltd","Website":"www.kcsl.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":24,"Name":"Larsen & Toubro 信息技术 Limite","Website":"www.lntinfotech.com","Address":"Mumbai","ImagePath":"http://test/lab/logos/"},{"ID":25,"Name":"Motorola 印度电子私人 有限公司","网站":"","地址":"班加罗尔","ImagePath":"http://test/lab/logos/"},{"ID":26,"名称":"Newgen Software Technologies Ltd","网站":"","地址":"新 德里","ImagePath":"http://test/lab/logos/"},{"ID":27,"Name":"NIIT 有限公司","网站":"www.niit.com","地址":"新 德里","ImagePath":"http://test/lab/logos/niit.png"},{"ID":28,"Name":"Novell 软件开发(一) Ltd","Website":"www.novell.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/novell.png"},{"ID":29,"Name":"Oracle 解决方案服务(印度)私人有限公司 Ltd","Website":"www.oracle.com","Address":"Bangalore","ImagePath":"http://test/lab/logos/"},{"ID":30,"Name":"Ramco 系统 Ltd","网站":"www.ramco.com","地址":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":31,"Name":"Sterling 信息技术 Limited","网站":"www.sterlingcarnegie.com","地址":"Chennai","ImagePath":"http://test/lab/logos/"},{"ID":32,"Name":"SRM 技术 Limited","Website":"www.srmsoft.com","Address":"chennai","ImagePath":"http://test/lab/logos/srm.jpg"},{"ID":33,"Name":"SAP 印度列兵 Ltd","Website":"www.sap.com/india","Address":"Bangalore","ImagePath":"http://test/lab/logos/sap.png"}]
【问题讨论】:
-
你的
json看起来怎么样? -
如果您同时包含
Json、`Xml' 和转换代码会有所帮助。 -
请发布您的完整 json。
标签: c# json xml xamarin.android