Search code examples
c#xmlxmlserializerxml-deserialization

"Unknown Node:VarName" during XML deserialization


I have this sample XML file which I need to deserialize:

<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
    <Unit>
        <StringVar>TohleJeString</StringVar>
        <Int32Var>24</Int32Var>
        <DoubleVar>29.6</DoubleVar>
        <DateTimeVar>17.11.1968</DateTimeVar>
        <CharVar>c</CharVar>
    </Unit>
    <Unit>
        <StringVar>TohleJeTakéString</StringVar>
        <Int32Var>17</Int32Var>
        <DoubleVar>5.9</DoubleVar>
        <DateTimeVar>06.07.1415</DateTimeVar>
        <CharVar>p</CharVar>
    </Unit>
    <Unit>
        <StringVar>NoATohleTaké</StringVar>
        <Int32Var>2</Int32Var>
        <DoubleVar>78.5</DoubleVar>
        <DateTimeVar>06.12.1774</DateTimeVar>
        <CharVar>x</CharVar>
    </Unit>
</CollectionOfUnits>

Here is the root class with the list of another class:

[XmlRoot("CollectionOfUnits", IsNullable = false)]
public class XmlExampleCollectionOfUnits
{
    [XmlArray("Unit")]
    public List<XmlExampleBasicUnit> Units { get; set; }
    public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
    {
        Units = units;
    }
    public XmlExampleCollectionOfUnits()
    {
    }
}

The structure of a class above:

public class XmlExampleBasicUnit
{
    public String StringVar { get; set; }
    public Int32 Int32Var { get; set; }
    public Double DoubleVar { get; set; }
    public DateTime DateTimeVar { get; set; }
    public Char CharVar { get; set; }
}

I use this functions from documentation of XmlSerializer which are helpful during debugging (this is from where is output called):

private void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
{
    Console.WriteLine("Unknown Node:" + e.Name + "\t" + e.Text);
}

private void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
{
    XmlAttribute attr = e.Attr;
    Console.WriteLine("Unknown attribute " + attr.Name + "='" + attr.Value + "'");
}

And here is the output:

Unknown Node:StringVar  
Unknown Node:Int32Var   
Unknown Node:DoubleVar  
Unknown Node:DateTimeVar    
Unknown Node:CharVar    
Unknown Node:StringVar  
Unknown Node:Int32Var   
Unknown Node:DoubleVar  
Unknown Node:DateTimeVar    
Unknown Node:CharVar    
Unknown Node:StringVar  
Unknown Node:Int32Var   
Unknown Node:DoubleVar  
Unknown Node:DateTimeVar    
Unknown Node:CharVar

I think the structure is coded correctly. Am I missing some designation?


Solution

  • There are 3 problems here;

    • you want XmlElementAttribute, not XmlArrayAttribute, as you only have a one-level nesting, so: [XmlElement("Unit")]
    • your dates are not valid as xml formatted dates; either format them appropriately (ISO 8601, so: 1968-11-17 etc), or use string instead of DateTime and convert it manually afterwards
    • the char type isn't a good fit - it is essentially an integer type; use string instead

    With those changes: it should work.