0

I have this xml from our vendor (an excerpt):

<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>

I have these classes set up in my c# code:

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; }
}

The xml I get from the vendor (via a web service), has 20 elements tagged Role and 6 tagged as immutable:Role. When I run my code, I only see the 20 Role items, but I want all 26 items. How can I go about getting them?

8
  • Did you try to copy the XML, go to Visual Studio : Edit -> Paste Special -> Paste XML as Classes. And try to deserialize them using that? Commented May 16, 2017 at 19:26
  • Nope. I didn't know about that. I'll try it. Commented May 16, 2017 at 19:34
  • I did that paste special and now I'm getting ambiguity errors. I'll muddle through and see if I can resolve them. Commented May 16, 2017 at 19:46
  • Okay - it was just a possible "quick win" - but it may not be the silver bullet here. Commented May 16, 2017 at 19:47
  • It's worth looking into, if for no other reason than getting the XP... Commented May 16, 2017 at 19:49

2 Answers 2

1

My weapon of choice for class generation is xsd, I find it much easier controlling attributes like that, rather than producing them on my own, especially in tricky scenarios like yours. Basically, it's a tale of two namespaces. Identical structure, different attribute decorations. There's 2 different namespaces no and imm to separate the Role classes. Item and Users nodes have the CT common type, and no.Role and imm.Role share this type.

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; }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just posting this out there for other folks, I found this website that will convert xml to c# objects. I like the results better than what Paste Special -> Paste XML as Classes was giving me. XML to c# website

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.