0

I have the following XML:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<accounts>
    <items>
        <account>
            <voornaam><FirstName</voornaam>
            <naam>LastName</naam>
            <gebruikersnaam>Username</gebruikersnaam>
            <internnummer></internnummer>
            <klasnummer></klasnummer>
        </account>
    </items>
</accounts>

With these classes:

public class Accounts
{
   public Accounts()
   {
      items = new List<Account>();
   }

   [XmlElement]
   public List<Account> items { get; set; }
}

public class Account
{
   public string voornaam { get; set; }
   public string naam { get; set; }
   public string gebruikersnaam { get; set; }
   public int? internnummer { get; set; }
   public int? klasnummer { get; set; }
}

And this code:

var decoded = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><accounts><items><account><voornaam>FirstName</voornaam><naam>LastName</naam><gebruikersnaam></gebruikersnaam><internnummer></internnummer><klasnummer></klasnummer></account></items></accounts>";
XmlSerializer serializer = new XmlSerializer(typeof(Accounts), new XmlRootAttribute("accounts"));
StringReader reader = new StringReader(decoded);
var accounts = (Accounts)serializer.Deserialize(reader);

All I get from this is an Accounts instance with the property Items containing one Account instance with every property null.

2
  • Aside from the given answer, I would recommend, as my personal opinion on the matter, to use another type of serialization, like JSON or something. XML is outdated and horrible to work with, lol... Commented Mar 8, 2022 at 2:10
  • @JuanR I would, but I get this data from a SOAP-webservice so not much I can do about it, unfortunately. Commented Mar 8, 2022 at 3:23

1 Answer 1

3

You need to specify the items as XML array with XmlArrayAttribute and also with the XmlArrayItemAttribute with (element) item's name: "account" and as Account type. XmlArrayAttribute Example.

public class Accounts
{
    [XmlArrayItem(ElementName= "account",
           Type = typeof(Account))]
    [XmlArray(ElementName="items")]
    public List<Account> items { get; set; }
}

Meanwhile, suggest using XmlElementAttribute to specify the element name in XML instead of naming the properties as camelCase.

For the reason why need InternnummerText and KlasnummerText properties you can refer to the question: Deserializing empty xml attribute value into nullable int property using XmlSerializer (will not cover in this answer).

public class Accounts
{
    public Accounts()
    {
        Items = new List<Account>();
    }

    [XmlArrayItem(ElementName= "account",
           Type = typeof(Account))]
    [XmlArray(ElementName="items")]
    public List<Account> Items { get; set; }
}

[XmlRoot(ElementName="account")]
public class Account
{
    [XmlIgnore]
    public int? Klasnummer { get; set; }
    [XmlIgnore]
    public int? Internnummer { get; set; }
    
    [XmlElement(ElementName="voornaam")]
    public string Voornaam { get; set; }
    [XmlElement(ElementName="naam")]
    public string Naam { get; set; }
    [XmlElement(ElementName="gebruikersnaam")]
    public string Gebruikersnaam { get; set; }
    [XmlElement(ElementName="internnummer")]
    public string InternnummerText 
    {
      get { return (Internnummer.HasValue) ? Internnummer.ToString() : null; } 
      set { Internnummer = !string.IsNullOrEmpty(value) ? int.Parse(value) : default(int?); }
    }
    [XmlElement(ElementName="klasnummer")]
    public string KlasnummerText 
    {
      get { return (Klasnummer.HasValue) ? Klasnummer.ToString() : null; } 
      set { Klasnummer = !string.IsNullOrEmpty(value) ? int.Parse(value) : default(int?); }
    }
}

Sample program

Sign up to request clarification or add additional context in comments.

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.