1

I want to deserialize this xml file

<?xml version="1.0" encoding="utf-16"?>
<ti:document xmlns:ti="http://www.to-increase.com/data/document">
  <Artikel>
    <Artikelnr>030-0003</Artikelnr>
    <Hoofd_EAN>2000003000036</Hoofd_EAN>
    <Artikelomschrijving>Promo Transformers Goodie</Artikelomschrijving>
    <Artikelomschrijving_lang>Promo Transformers Goodiebag Poster+Wobbler </Artikelomschrijving_lang>
    <Gewicht>0</Gewicht>
    <Lengte>0</Lengte>
    <Breedte>0</Breedte>
    <Hoogte>0</Hoogte>
    <Extra_EAN_codes>
      <Extra_EAN_code>2000003000036</Extra_EAN_code>
    </Extra_EAN_codes>
  </Artikel>
  <Artikel>
    <Artikelnr>030-0006</Artikelnr>
    <Hoofd_EAN>2000003000067</Hoofd_EAN>
    <Artikelomschrijving>Promo 2e Spel 50% Actie</Artikelomschrijving>
    <Artikelomschrijving_lang>Promo 2e Spel 50% Actie </Artikelomschrijving_lang>
    <Gewicht>0</Gewicht>
    <Lengte>0</Lengte>
    <Breedte>0</Breedte>
    <Hoogte>0</Hoogte>
    <Extra_EAN_codes>
      <Extra_EAN_code>2000003000067</Extra_EAN_code>
    </Extra_EAN_codes>
  </Artikel>
  <Artikel>
    <Artikelnr>030-0007</Artikelnr>
    <Hoofd_EAN>2000003000074</Hoofd_EAN>
    <Artikelomschrijving>Promo Lego Spaaractie Kop</Artikelomschrijving>
    <Artikelomschrijving_lang>Promo Lego Spaaractie Kopkaart </Artikelomschrijving_lang>
    <Gewicht>80</Gewicht>
    <Lengte>103</Lengte>
    <Breedte>73</Breedte>
    <Hoogte>1</Hoogte>
    <Extra_EAN_codes>
      <Extra_EAN_code>2000003000074</Extra_EAN_code>
    </Extra_EAN_codes>
  </Artikel>
</ti:document>

this is the code to deserialize the xml into an Artikels object.

[Serializable]
[XmlRootAttribute(Namespace = "http://www.to-increase.com/data/document", ElementName = "document")]
public class Artikels
{
    public Artikels()
    {
        Artikelen = new List<Artikel>();
    }

    [XmlElement("Artikel")]
    public List<Artikel> Artikelen { get; set; }

    private static XmlSerializer _serializer;
    private static XmlSerializer Serializer
    {
        get
        {
            if ((_serializer == null))
            {
                _serializer = new XmlSerializer(typeof(Artikels));
            }
            return _serializer;
        }
    }

    /// <summary>
    /// Deserializes workflow markup into an orders object
    /// </summary>
    /// <param name="xml">string workflow markup to deserialize</param>
    /// <param name="obj">Output orders object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool Deserialize(string xml, out Artikels obj, out Exception exception)
    {
        exception = null;
        obj = default(Artikels);
        try
        {
            obj = Deserialize(xml);
            return true;
        }
        catch (Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool Deserialize(string xml, out Artikels obj)
    {
        Exception exception;
        return Deserialize(xml, out obj, out exception);
    }

    public static Artikels Deserialize(string xml)
    {
        using (var sr = new StringReader(xml))
        {
            return ((Artikels)(Serializer.Deserialize(XmlReader.Create(sr))));
        }
    }

    /// <summary>
    /// Deserializes xml markup from file into an orders object
    /// </summary>
    /// <param name="fileName">string xml file to load and deserialize</param>
    /// <param name="obj">Output orders object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool LoadFromFile(string fileName, out Artikels obj, out Exception exception)
    {
        exception = null;
        obj = default(Artikels);
        try
        {
            obj = LoadFromFile(fileName);
            return true;
        }
        catch (Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool LoadFromFile(string fileName, out Artikels obj)
    {
        Exception exception;
        return LoadFromFile(fileName, out obj, out exception);
    }

    public static Artikels LoadFromFile(string fileName)
    {
        string xmlString;
        using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            using (var sr = new StreamReader(file))
            {
                xmlString = sr.ReadToEnd();
            }
        }

        if (!string.IsNullOrEmpty(xmlString))
        {
            return Deserialize(xmlString);
        }

        return null;
    }
}

[Serializable]
public class Artikel
{
    public string Artikelnr { get; set; }
    public string Hoofd_EAN { get; set; }
    public string Artikelomschrijving { get; set; }
    public string Artikelomschrijving_lang { get; set; }
    public int Gewicht { get; set; }
    public int Lenghte { get; set; }
    public int Breedte { get; set; }
    public int Hoogte { get; set; }
    [XmlArrayItem("Extra_EAN_code", IsNullable = true)]
    public List<string> Extra_EAN_codes { get; set; }
}

Calling function

                Artikels artikelen;
            Exception ex;
            if (!Artikels.LoadFromFile(filePath, out artikelen, out ex))
            {
                ProcessError(errorDir, filePath, fileName, ex.Message);
                continue;
            }

The code runs fine but the list of Artikelen stays empty. If I remove the namespace attribute and change the xml file to <document> as root element everything is deserialized as expected. How do i get it to work with the namespace?

swd
  • 23
  • 1
  • 5
  • possible duplicate of [How do I Deserialize xml document with namespaces using XmlSerializer?](http://stackoverflow.com/questions/7795958/how-do-i-deserialize-xml-document-with-namespaces-using-xmlserializer) – Evan Mulawski Jul 23 '15 at 18:50
  • Element name must match the tag name in XML and is case sensitive. You do not have ElementName = "document", you need "Artikels". – jdweng Jul 23 '15 at 18:55

1 Answers1

4

In your XML, the document element is in the "http://www.to-increase.com/data/document" namespace with prefix ti, but the child Artikel elements are not in any namespace. Therefore you need to decorate the Artikelen property as follows:

    [XmlElement("Artikel", Namespace="")]
    public List<Artikel> Artikelen { get; set; }

(If the root element had a default namespace of xmlns="http://www.to-increase.com/data/document", then the child elements would have been in that namespace. However, there is no default namespace, only the named ti namespace.)

dbc
  • 104,963
  • 20
  • 228
  • 340