[英]How to serialize attribute
I am trying to deserialize this code 我试图反序列化此代码
<request>
<employee id="40407">Test User</employee>
</request>
I've created a class: 我创建了一个类:
public class Request
{
public string employee { get; set; }
[XmlAttribute("employee/id")]
public string employeeId { get; set; }
}
Without attribute everything works, but I need the data from attribute " id
" as well. 没有属性一切正常,但我也需要来自属性“
id
”的数据。 Once I put [XmlAttribute("employee/id")]
it doesn't want to work. 一旦我把
[XmlAttribute("employee/id")]
它不想工作。 What I am doing wrong? 我做错了什么?
I think you need following classes to deserialize that xml: 我认为您需要以下类来反序列化该xml:
[XmlRoot("request")]
public class Request
{
[XmlElement("employee")]
public Employee Employee { get; set; }
}
[XmlRoot("employee")]
public class Employee
{
[XmlText]
public string Name { get; set; }
[XmlAttribute("id")]
public string EmployeeId { get; set; }
}
public class empl
{
[XmlText]
public string name { get; set; }
[XmlAttribute]
public int id { get; set; }
}
public class request
{
public empl employee { get; set; }
}
public Test()
{
XmlSerializer ser = new XmlSerializer(typeof(request));
MemoryStream mem = new MemoryStream();
ser.Serialize(mem , new request { employee = new empl { name="ff", id=6}});
string dec = UTF8Encoding.UTF8.GetString(mem.ToArray());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.