csharp 序列化XML,将XMLfeed转换为模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 序列化XML,将XMLfeed转换为模型相关的知识,希望对你有一定的参考价值。
//model
using System.Xml.Serialization;
namespace CvLibrary.Models
{
[Serializable, XmlRoot("jobs")]
public class JobViewModel
{
[XmlElement("job")]
public List<Job> Job { get; set; }
}
public class Job
{
[XmlElement("jobref")]
public int JobRef { get; set; }
[XmlElement("date")]
public string Date { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("email")]
public string Email { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("salarymin")]
public string SalaryMin { get; set; }
[XmlElement("salarymax")]
public string SalaryMax { get; set; }
[XmlElement("benefits")]
public string Benefits { get; set; }
[XmlElement("salary")]
public string Salary { get; set; }
[XmlElement("jobtype")]
public string JobType { get; set; }
[XmlElement("full_part")]
public string Full_Part { get; set; }
[XmlElement("salary_per")]
public string Salary_Per { get; set; }
[XmlElement("location")]
public string Location { get; set; }
[XmlElement("country")]
public string Country { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("category")]
public string Category { get; set; }
[XmlElement("image")]
public string Image { get; set; }
}
}
//serialize method
private void SerializeXmlFeed(XmlReader reader, FeedConfigurationViewModel config)
{
JobViewModel record = new JobViewModel();
XmlSerializer serializer = new XmlSerializer(typeof(JobViewModel));
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
record = (JobViewModel)serializer.Deserialize(fileStream);
}
}
//loop each record
foreach (var item in record.Job)
{
//code here
}
以上是关于csharp 序列化XML,将XMLfeed转换为模型的主要内容,如果未能解决你的问题,请参考以下文章
csharp 此类演示了将对象序列化和反序列化为xml字符串的方法
XStream将XML转换为JAVA对象快速指南
使用 Json.Net 转换为 XML 时如何序列化空数组
将 XML 响应转换为 Json 时出错
将对象转换为 XML 字符串
csharp 从XML反序列化