[英]RestSharp XML to Object
我正在使用RestSharp来调用Web服务。 该服务返回一个XML文件,我想将其转换为C#对象。
XML文件如下所示,但包含多个作业:
<jobs>
<job>
<standard_fields>
<start_date>2015-04-23</start_date>
<end_date>2015-05-23</end_date>
<title>Ad Titel</title>
<reference>Test offer Dutch-9483599</reference>
<job_description>...</job_description>
<profile_description>...</profile_description>
<company_description>...</company_description>
<company_name>MLV Partners</company_name>
<application_email>offre9483599.9252@mlvpartners.contactrh.com</application_email>
<education_level id="6">Specialisatie</education_level>
<work_experience id="6">4-5 jaar</work_experience>
<contract_type id="2">Tijdelijk</contract_type>
<location>...</location>
<function id="20500">Informatiesystemen / telecommunicatie</function>
<sub_function id="20518">Ingenieur Studies / Ontwikkeling</sub_function>
<sector id="40000">Informatica / Telecommunicatie / Internet</sector>
<sub_sector id="40002">Software-uitgever</sub_sector>
<id>9483599</id>
</standard_fields>
<custom_fields>
<recruiter_id>Recruiter ID</recruiter_id>
</custom_fields>
</job>
<jobs>
虽然我的C#类看起来像这样:
public class job
{
[DeserializeAs(Name = "standard_fields")]
public StandardFields standard_fields { get; set; }
[DeserializeAs(Name = "custom_fields")]
public CustomFields custom_fields { get; set; }
}
[DeserializeAs(Name = "custom_fields")]
public class CustomFields
{
public string recruiter_id { get; set; }
}
[DeserializeAs(Name = "standard_fields")]
public class StandardFields
{
public DateTime start_date { get; set; }
public DateTime end_date { get; set; }
public string title { get; set; }
public string reference { get; set; }
public string job_description { get; set; }
public string profile_description { get; set; }
public string company_description { get; set; }
public string company_name { get; set; }
public string application_email { get; set; }
public string education_level { get; set; }
public string work_experience { get; set; }
public string contract_type { get; set; }
public string location { get; set; }
public string function { get; set; }
public string sub_function { get; set; }
public string sector { get; set; }
public string sub_sector { get; set; }
public int id { get; set; }
}
我认为我的C#类与XML结构匹配。 所以现在,我所要做的就是调用API。
var client = new RestClient("....");
var request = new RestRequest("...");
request.OnBeforeDeserialization = resp =>
{
resp.ContentType = "application/json";
};
request.RootElement = "jobs";
var listJob = client.Execute<List<job>>(request);
Console.WriteLine(listJob.Data.ToString());
但是这无效, listJob
为空,RestSharp请求给出错误信息: 无效的JSON字符串 。
有人可以帮我找到解决这个问题的方法吗?
编辑
好的,我收到了我的愚蠢错误并将其删除。
request.OnBeforeDeserialization = resp =>
{
resp.ContentType = "application/json";
};
现在,我正在尝试使用列表。
最终编辑
问题解决了 !
var client = new RestClient("...");
var request = new RestRequest("...");
var listJob = client.Execute<List<job>>(request);
foreach (var job in listJob.Data)
{
Console.WriteLine(job.standard_fields.reference);
}
尝试这个
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication19 { class Program { const string FILENAME = @"c:\\temp\\Test.xml"; static void Main(string[] args) { jobs jobList = new jobs(){ jobList = new List<job>(){ new job() { standard_fields = new StandardFields{ start_date = DateTime.Parse("2015-04-23"), end_date = DateTime.Parse("2015-05-23"), title = "Ad Titel", reference = "Test offer Dutch-9483599", job_description = "...", profile_description = "...", company_description = "...", company_name = "MLV Partners", application_email = "offre9483599.9252@mlvpartners.contactrh.com", education_level = new CID() {id = 6, value = "Specialisatie"}, work_experience = new CID() {id = 6, value = "4-5 jaar"}, contract_type = new CID() {id = 2, value = "Tijdelijk"}, location = "...", function = new CID() {id = 20500, value = "Informatiesystemen / telecommunicatie"}, sub_function = new CID() {id = 20518, value = "Ingenieur Studies / Ontwikkeling"}, sector = new CID() { id = 40000, value = "Informatica"}, sub_sector = new CID() { id = 40002, value = "Software-uitgever"}, id = 9483599 }, custom_fields = new CustomFields(){ recruiter_id = "Recruiter ID" } } } }; XmlSerializer serializer = new XmlSerializer(typeof(jobs)); StreamWriter writer = new StreamWriter(FILENAME); XmlSerializerNamespaces _ns = new XmlSerializerNamespaces(); //_ns.Add("", ""); //serializer.Serialize(writer, icp, _ns); serializer.Serialize(writer, jobList); writer.Flush(); writer.Close(); writer.Dispose(); XmlSerializer xs = new XmlSerializer(typeof(jobs)); XmlTextReader reader = new XmlTextReader(FILENAME); jobs newJobs = (jobs)xs.Deserialize(reader); } } [XmlRoot("jobs")] public class jobs { [XmlElement("job")] public List<job> jobList { get; set; } } [XmlRoot("job")] public class job { [XmlElement("standard_fields")] public StandardFields standard_fields { get; set; } [XmlElement("custom_fields")] public CustomFields custom_fields { get; set; } } [XmlRoot("custom_fields")] public class CustomFields { public string recruiter_id { get; set; } } [XmlRoot("standard_fields")] public class StandardFields { [XmlElement("start_date")] public DateTime start_date { get; set; } [XmlElement("end_date")] public DateTime end_date { get; set; } [XmlElement("title")] public string title { get; set; } [XmlElement("reference")] public string reference { get; set; } [XmlElement("job_description")] public string job_description { get; set; } [XmlElement("profile_description")] public string profile_description { get; set; } [XmlElement("company_description")] public string company_description { get; set; } [XmlElement("company_name")] public string company_name { get; set; } [XmlElement("application_email")] public string application_email { get; set; } [XmlElement("education_level")] public CID education_level { get; set; } [XmlElement("work_experience")] public CID work_experience { get; set; } [XmlElement("contract_type")] public CID contract_type { get; set; } [XmlElement("location")] public string location { get; set; } [XmlElement("function")] public CID function { get; set; } [XmlElement("sub_function")] public CID sub_function { get; set; } [XmlElement("sector")] public CID sector { get; set; } [XmlElement("sub_sector")] public CID sub_sector { get; set; } [XmlElement("id")] public int id { get; set; } } public class CID { [XmlText] public string value { get; set; } [XmlAttribute("id")] public int id { get; set; } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.