I'm using RestSharp to make a call to a web service. The service returns an XML file which I want to convert to a C# object.
The XML file looks this but contains multiple jobs:
<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>
While my C# class looks like this:
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; }
}
I think that my C# class matches the XML structure. So now, all I should have to do is call the 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());
This doesn't work though , listJob
is null and the RestSharp request gives the error message Invalid JSON string .
Could someone help me find a solution to this problem?
EDIT
Ok , I got my stupid error and removed this.
request.OnBeforeDeserialization = resp =>
{
resp.ContentType = "application/json";
};
Now i'm trying to work with a list.
FINAL EDIT
Problem solved !
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);
}
Try this
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; } } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.