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.

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
Rnoo
  • 21
  • 3
  • 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... – JuanR Mar 08 '22 at 02:10
  • @JuanR I would, but I get this data from a SOAP-webservice so not much I can do about it, unfortunately. – Rnoo Mar 08 '22 at 03:23

1 Answers1

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

Yong Shun
  • 35,286
  • 4
  • 24
  • 46