XmlSerializer - 通用列表的元素名称 class
XmlSerializer - Element name for generic list class
我正在尝试使用 XmlSerializer
将对象序列化为 XML
我期待得到这样的东西:
<User>
<username>administrator</username>
<email>administrator@email.com</email>
<roles>
<whatever id="D3DCBCDA-AD61-4764-B5A1-057D654F1C26">
<role>admin</role>
</whatever>
</roles>
</user>
但我得到的是:
<User>
<username>administrator</username>
<email>administrator@email.com</email>
<roles>
<RoleDTO id="D3DCBCDA-AD61-4764-B5A1-057D654F1C26">
<role>admin</role>
</RoleDTO>
</roles>
</User>
代码
XmlSerializer x = new System.Xml.Serialization.XmlSerializer(xml.GetType());
XmlSerializer xsSubmit = new XmlSerializer(typeof(UserDTO));
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww);
xsSubmit.Serialize(writer, user);
var xmll = sww.ToString(); // My xml
List<RoleDTO> rl = new List<RoleDTO>();
rl.Add(new RoleDTO { roleId = new Guid("D3DCBCDA-AD61-4764-B5A1-057D654F1C26"), role = "admin" });
UserDTO user = new UserDTO
{
username = "administrator",
email = "administrator@email.com",
role = rl
};
型号
namespace Model.DTO
{
[XmlRoot("User")]
public class UserDTO
{
public string username { get; set; }
public string email { get; set; }
public List<RoleDTO> role { get; set; }
}
public class RoleDTO
{
[XmlAttribute("Id")]
public Guid roleId { get; set; }
public string role { get; set; }
}
}
我尝试用 [XmlRoot("whatever")]
装饰 class RoleDTO,[XmlElement("whatever")]
但没有成功。
有什么想法吗?
您需要用
修饰用户 DTO class 的角色 属性
[XmlElement("whatever")]
不是 RoleDTO class。
public class UserDTO
{
public string username { get; set; }
public string email { get; set; }
[XmlElement("whatever")]
public List<RoleDTO> role { get; set; }
}
我正在尝试使用 XmlSerializer
将对象序列化为 XML我期待得到这样的东西:
<User>
<username>administrator</username>
<email>administrator@email.com</email>
<roles>
<whatever id="D3DCBCDA-AD61-4764-B5A1-057D654F1C26">
<role>admin</role>
</whatever>
</roles>
</user>
但我得到的是:
<User>
<username>administrator</username>
<email>administrator@email.com</email>
<roles>
<RoleDTO id="D3DCBCDA-AD61-4764-B5A1-057D654F1C26">
<role>admin</role>
</RoleDTO>
</roles>
</User>
代码
XmlSerializer x = new System.Xml.Serialization.XmlSerializer(xml.GetType());
XmlSerializer xsSubmit = new XmlSerializer(typeof(UserDTO));
StringWriter sww = new StringWriter();
XmlWriter writer = XmlWriter.Create(sww);
xsSubmit.Serialize(writer, user);
var xmll = sww.ToString(); // My xml
List<RoleDTO> rl = new List<RoleDTO>();
rl.Add(new RoleDTO { roleId = new Guid("D3DCBCDA-AD61-4764-B5A1-057D654F1C26"), role = "admin" });
UserDTO user = new UserDTO
{
username = "administrator",
email = "administrator@email.com",
role = rl
};
型号
namespace Model.DTO
{
[XmlRoot("User")]
public class UserDTO
{
public string username { get; set; }
public string email { get; set; }
public List<RoleDTO> role { get; set; }
}
public class RoleDTO
{
[XmlAttribute("Id")]
public Guid roleId { get; set; }
public string role { get; set; }
}
}
我尝试用 [XmlRoot("whatever")]
装饰 class RoleDTO,[XmlElement("whatever")]
但没有成功。
有什么想法吗?
您需要用
修饰用户 DTO class 的角色 属性[XmlElement("whatever")]
不是 RoleDTO class。
public class UserDTO
{
public string username { get; set; }
public string email { get; set; }
[XmlElement("whatever")]
public List<RoleDTO> role { get; set; }
}