我有来自供应商的这个xml(摘录):
<Roles xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:immutable="http://www.digitalmeasures.com/schema/immutable">
<immutable:Role roleKey="INDIVIDUAL-ACTIVITIES-University-DataBackupService" text="Data Backup Service">
<Item xlink:type="simple" xlink:href="/login/service/v4/Role/INDIVIDUAL-ACTIVITIES-University-DataBackupService"/>
<Users xlink:type="simple" xlink:href="/login/service/v4/RoleUser/INDIVIDUAL-ACTIVITIES-University-DataBackupService"/>
</immutable:Role>
<Role roleKey="INDIVIDUAL-ACTIVITIES-University-DepartmentUpdatePrimaryAssignmentOrg" text="Department: Update Primary Assignment Org">
<Item xlink:type="simple" xlink:href="/login/service/v4/Role/INDIVIDUAL-ACTIVITIES-University-DepartmentUpdatePrimaryAssignmentOrg"/>
<Users xlink:type="simple" xlink:href="/login/service/v4/RoleUser/INDIVIDUAL-ACTIVITIES-University-DepartmentUpdatePrimaryAssignmentOrg"/>
</Role>
</Roles>
我在c#代码中设置了这些类:
public class Role
{
[XmlAttribute]
public string roleKey { get; set; }
[XmlAttribute]
public string text { get; set; }
[XmlAttribute]
public string Item { get; set; }
[XmlAttribute]
public string Users { get; set; }
}
//Class to hold our array of <DailyStat>
[Serializable]
[XmlRootAttribute("Roles")]
//[XmlRootAttribute("immutable:Roles")]
public class Roles
{
[XmlElement("Role")]
public Role[] thisRole { get; set; }
}
我从供应商处获得的xml(通过Web服务)有20个标记为Role的元素,6个标记为immutable:Role。当我运行我的代码时,我只看到20个角色项目,但我想要所有26个项目。我怎样才能得到它们?
答案 0 :(得分:1)
我为类生成选择的武器是xsd
,我发现控制这样的属性要容易得多,而不是自己生成它们,特别是在像你这样的棘手场景中。基本上,它是两个名称空间的故事。结构相同,属性装饰不同。有两个不同的命名空间no
和imm
来分隔Role
类。 Item
和Users
个节点具有CT
常见类型,no.Role
和imm.Role
共享此类型。
internal static class ct
{
public const string nsImmutable = "http://www.digitalmeasures.com/schema/immutable";
public const string nsXLink = "http://www.w3.org/1999/xlink";
}
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public partial class Roles
{
[XmlElement("Role", typeof(no.Role))]
[XmlElement("Role", typeof(imm.Role), Namespace = ct.nsImmutable)]
public object[] Items { get; set; }
}
public partial class BaseRole
{
[XmlAttribute("roleKey")]
public string RoleKey { get; set; }
[XmlAttribute("text")]
public string Text { get; set; }
}
[Serializable]
[XmlType(AnonymousType = true)]
//[XmlRoot(Namespace = "", IsNullable = false)]
public partial class CT
{
[XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = ct.nsXLink, AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = ct.nsXLink, AttributeName = "href")]
public string Href { get; set; }
}
namespace imm
{
[Serializable]
[XmlType(AnonymousType = true, Namespace = ct.nsImmutable)]
[XmlRoot(Namespace = ct.nsImmutable, IsNullable = false)]
public partial class Role : BaseRole
{
[XmlElement(Namespace = "", Type = typeof(CT), ElementName = "Item")]
public CT Item { get; set; }
[XmlElement(Namespace = "", Type = typeof(CT), ElementName = "Users")]
public CT Users { get; set; }
}
}
namespace no
{
[Serializable]
[XmlType(AnonymousType = true)]
public partial class Role : BaseRole
{
[XmlElement("Item", typeof(CT))]
public CT Item { get; set; }
[XmlElement("Users", typeof(CT))]
public CT Users { get; set; }
}
}
答案 1 :(得分:0)
只是将其发布给其他人,我发现这个网站将xml转换为c#对象。我喜欢的结果比Paste Special更好 - &gt;将XML粘贴为Classes给了我。 XML to c# website