如何序列化属性
本文关键字:属性 序列化 | 更新日期: 2023-09-27 17:50:06
我正在尝试反序列化这段代码
<request>
<employee id="40407">Test User</employee>
</request>
我创建了一个类:
public class Request
{
public string employee { get; set; }
[XmlAttribute("employee/id")]
public string employeeId { get; set; }
}
没有属性一切工作,但我需要属性"id
"的数据,以及。一旦我加入[XmlAttribute("employee/id")]
,它就不工作了。我做错了什么?
我认为您需要以下类来反序列化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());
}