使用 XmlSerializer C# 将数据从 XML 加载到 Class
Load data from XML to Class using XmlSerializer C#
我有一个 XML 如下所示。
<?xml version="1.0" encoding="utf-8"?>
<RoleManagement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Permissions>
<Group Name="Test Case Management">
<Permission ID="1" Name="Add/Edit/Delete test cases" />
<Permission ID="2" Name="Assign Test cases to users" />
</Group>
<Group Name="Bug Management">
<Permission ID="9" Name="Add/Edit/Delete bugs" />
<Permission ID="13" Name="View bugs" />
</Group>
<Group Name="Administration">
<Permission ID="19" Name="Database backup" />
<Permission ID="20" Name="Role Management" />
</Group>
</Permissions>
<RolePermissions>
<RolePermission RedmineId="8" PermissionID="9" />
<RolePermission RedmineId="8" PermissionID="13" />
</RolePermissions>
<Roles>
<Role Name="Manager" RedmineId="8" TestlinkId="15" />
</Roles>
</RoleManagement>
我需要使用 XmlSerializer
.
将数据从 xml 加载到 C# 类
到目前为止我做了什么
我创造了这些类。
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
public partial class RoleManagement
{
private List<Group> permissionGroupsField;
private List<RolePermission> rolePermissionsField;
private List<Role> rolesField;
[System.Xml.Serialization.XmlArrayItem("Group", IsNullable = false)]
public List<Group> PermissionGroups
{
get { return this.permissionGroupsField; }
set { this.permissionGroupsField = value; }
}
[System.Xml.Serialization.XmlArrayItem("RolePermission", IsNullable = false)]
public List<RolePermission> RolePermissions
{
get { return this.rolePermissionsField; }
set { this.rolePermissionsField = value; }
}
[System.Xml.Serialization.XmlArrayItem("Role", IsNullable = false)]
public List<Role> Roles
{
get { return this.rolesField; }
set { this.rolesField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Group
{
private List<Permission> permissionsField;
private string nameField;
[System.Xml.Serialization.XmlElement("Permission")]
public List<Permission> Permissions
{
get { return this.permissionsField; }
set { this.permissionsField = value; }
}
[System.Xml.Serialization.XmlAttribute("Name")]
public string GroupName
{
get { return this.nameField; }
set { this.nameField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Permission
{
private int idField;
private string nameField;
[System.Xml.Serialization.XmlAttribute()]
public int ID
{
get { return this.idField; }
set { this.idField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get { return this.nameField; }
set { this.nameField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class RolePermission
{
private int redmineIdField;
private int permissionIDField;
[System.Xml.Serialization.XmlAttribute()]
public int RedmineId
{
get { return this.redmineIdField; }
set { this.redmineIdField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int PermissionID
{
get { return this.permissionIDField; }
set { this.permissionIDField = value; }
}
}
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Role
{
private string nameField;
private int redmineIdField;
private int testlinkIdField;
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get { return this.nameField; }
set { this.nameField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int RedmineId
{
get { return this.redmineIdField; }
set { this.redmineIdField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int TestlinkId
{
get { return this.testlinkIdField; }
set { this.testlinkIdField = value; }
}
}
我的问题
已为 <RolePermissions>
节点和 <Roles>
节点正确加载数据。但是 <Permissions>
节点的数据根本没有加载。它始终为空。
为了从另一个列表中的列表获取数据,我还需要添加什么吗?
有什么想念的吗?
其中一个属性命名不正确 - 名称必须与父节点名称匹配。
你有 <Permissions>
所以 属性 应该被命名为相同的。所以改变这一行:
public List<Group> PermissionGroups
至此
public List<Group> Permissions
使用这些 类 我能够正确反序列化 XML:
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class RoleManagement
{
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Group", IsNullable = false)]
public List<RoleManagementGroup> Permissions { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("RolePermission", IsNullable = false)]
public List<RoleManagementRolePermission> RolePermissions { get; set; }
/// <remarks/>
public RoleManagementRoles Roles { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementGroup
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Permission")]
public List<RoleManagementGroupPermission> Permission { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementGroupPermission
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte ID { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRolePermission
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte RedmineId { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte PermissionID { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRoles
{
/// <remarks/>
public RoleManagementRolesRole Role { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRolesRole
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte RedmineId { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte TestlinkId { get; set; }
}
测试代码:
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoleManagement xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Permissions>
<Group Name=""Test Case Management"">
<Permission ID=""1"" Name=""Add/Edit/Delete test cases"" />
<Permission ID=""2"" Name=""Assign Test cases to users"" />
</Group>
<Group Name=""Bug Management"">
<Permission ID=""9"" Name=""Add/Edit/Delete bugs"" />
<Permission ID=""13"" Name=""View bugs"" />
</Group>
<Group Name=""Administration"">
<Permission ID=""19"" Name=""Database backup"" />
<Permission ID=""20"" Name=""Role Management"" />
</Group>
</Permissions>
<RolePermissions>
<RolePermission RedmineId=""8"" PermissionID=""9"" />
<RolePermission RedmineId=""8"" PermissionID=""13"" />
</RolePermissions>
<Roles>
<Role Name=""Manager"" RedmineId=""8"" TestlinkId=""15"" />
</Roles>
</RoleManagement>";
RoleManagement roleManagement;
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(xml);
using (MemoryStream stream = new MemoryStream(byteArray))
{
XmlSerializer serializer = new XmlSerializer(typeof(RoleManagement));
using (XmlReader reader = XmlReader.Create(stream))
{
roleManagement = (RoleManagement)serializer.Deserialize(reader);
}
}
我有一个 XML 如下所示。
<?xml version="1.0" encoding="utf-8"?>
<RoleManagement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Permissions>
<Group Name="Test Case Management">
<Permission ID="1" Name="Add/Edit/Delete test cases" />
<Permission ID="2" Name="Assign Test cases to users" />
</Group>
<Group Name="Bug Management">
<Permission ID="9" Name="Add/Edit/Delete bugs" />
<Permission ID="13" Name="View bugs" />
</Group>
<Group Name="Administration">
<Permission ID="19" Name="Database backup" />
<Permission ID="20" Name="Role Management" />
</Group>
</Permissions>
<RolePermissions>
<RolePermission RedmineId="8" PermissionID="9" />
<RolePermission RedmineId="8" PermissionID="13" />
</RolePermissions>
<Roles>
<Role Name="Manager" RedmineId="8" TestlinkId="15" />
</Roles>
</RoleManagement>
我需要使用 XmlSerializer
.
到目前为止我做了什么
我创造了这些类。
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
public partial class RoleManagement
{
private List<Group> permissionGroupsField;
private List<RolePermission> rolePermissionsField;
private List<Role> rolesField;
[System.Xml.Serialization.XmlArrayItem("Group", IsNullable = false)]
public List<Group> PermissionGroups
{
get { return this.permissionGroupsField; }
set { this.permissionGroupsField = value; }
}
[System.Xml.Serialization.XmlArrayItem("RolePermission", IsNullable = false)]
public List<RolePermission> RolePermissions
{
get { return this.rolePermissionsField; }
set { this.rolePermissionsField = value; }
}
[System.Xml.Serialization.XmlArrayItem("Role", IsNullable = false)]
public List<Role> Roles
{
get { return this.rolesField; }
set { this.rolesField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Group
{
private List<Permission> permissionsField;
private string nameField;
[System.Xml.Serialization.XmlElement("Permission")]
public List<Permission> Permissions
{
get { return this.permissionsField; }
set { this.permissionsField = value; }
}
[System.Xml.Serialization.XmlAttribute("Name")]
public string GroupName
{
get { return this.nameField; }
set { this.nameField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Permission
{
private int idField;
private string nameField;
[System.Xml.Serialization.XmlAttribute()]
public int ID
{
get { return this.idField; }
set { this.idField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get { return this.nameField; }
set { this.nameField = value; }
}
}
[Serializable()]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class RolePermission
{
private int redmineIdField;
private int permissionIDField;
[System.Xml.Serialization.XmlAttribute()]
public int RedmineId
{
get { return this.redmineIdField; }
set { this.redmineIdField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int PermissionID
{
get { return this.permissionIDField; }
set { this.permissionIDField = value; }
}
}
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class Role
{
private string nameField;
private int redmineIdField;
private int testlinkIdField;
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get { return this.nameField; }
set { this.nameField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int RedmineId
{
get { return this.redmineIdField; }
set { this.redmineIdField = value; }
}
[System.Xml.Serialization.XmlAttribute()]
public int TestlinkId
{
get { return this.testlinkIdField; }
set { this.testlinkIdField = value; }
}
}
我的问题
已为 <RolePermissions>
节点和 <Roles>
节点正确加载数据。但是 <Permissions>
节点的数据根本没有加载。它始终为空。
为了从另一个列表中的列表获取数据,我还需要添加什么吗?
有什么想念的吗?
其中一个属性命名不正确 - 名称必须与父节点名称匹配。
你有 <Permissions>
所以 属性 应该被命名为相同的。所以改变这一行:
public List<Group> PermissionGroups
至此
public List<Group> Permissions
使用这些 类 我能够正确反序列化 XML:
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class RoleManagement
{
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Group", IsNullable = false)]
public List<RoleManagementGroup> Permissions { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("RolePermission", IsNullable = false)]
public List<RoleManagementRolePermission> RolePermissions { get; set; }
/// <remarks/>
public RoleManagementRoles Roles { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementGroup
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Permission")]
public List<RoleManagementGroupPermission> Permission { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementGroupPermission
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte ID { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRolePermission
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte RedmineId { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte PermissionID { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRoles
{
/// <remarks/>
public RoleManagementRolesRole Role { get; set; }
}
/// <remarks/>
[Serializable]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class RoleManagementRolesRole
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte RedmineId { get; set; }
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte TestlinkId { get; set; }
}
测试代码:
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<RoleManagement xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Permissions>
<Group Name=""Test Case Management"">
<Permission ID=""1"" Name=""Add/Edit/Delete test cases"" />
<Permission ID=""2"" Name=""Assign Test cases to users"" />
</Group>
<Group Name=""Bug Management"">
<Permission ID=""9"" Name=""Add/Edit/Delete bugs"" />
<Permission ID=""13"" Name=""View bugs"" />
</Group>
<Group Name=""Administration"">
<Permission ID=""19"" Name=""Database backup"" />
<Permission ID=""20"" Name=""Role Management"" />
</Group>
</Permissions>
<RolePermissions>
<RolePermission RedmineId=""8"" PermissionID=""9"" />
<RolePermission RedmineId=""8"" PermissionID=""13"" />
</RolePermissions>
<Roles>
<Role Name=""Manager"" RedmineId=""8"" TestlinkId=""15"" />
</Roles>
</RoleManagement>";
RoleManagement roleManagement;
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(xml);
using (MemoryStream stream = new MemoryStream(byteArray))
{
XmlSerializer serializer = new XmlSerializer(typeof(RoleManagement));
using (XmlReader reader = XmlReader.Create(stream))
{
roleManagement = (RoleManagement)serializer.Deserialize(reader);
}
}