2

I'm not able to deserialize the following secition of an XML with C#

<mainfile>
    <portfolio>
        <fotos>
            <foto> <!CDATA[https://whatever.com/fotos/E/400/photo.JPG]]>
            </foto>
        </fotos>
    </portfolio>
    <portfolio>
        <fotos>
            <foto> <!CDATA[https://whatever.com/fotos/E/400/photo1.JPG]]>
            </foto>
        </fotos>
    </portfolio>
</mainfile>

I think it should be quite straight forward, but when deserializing it always returns an empty list. Here is the code:

[XmlRoot("mainfile")]
public class MainFile
{
    public MainFile()
    {
        porftolios= new List<Portfolio>();
    }

    [XmlElement("portfolio")]
    public List<Portfolio> Portfolios{ get; set; }
}

public class Portfolio
{
    ....
    [XmlElement("fotos")]
    public List<Foto> Fotos { get; set; }
}

public class Foto
{
    [XmlText]
    public string data{ get; set; }
}

Thanks.

EDIT. From HimBromBeere's solution i've chaged the following code, with a successful result:

public class Portfolio
{
    ....
    [XmlArray("fotos")]
    [XmlArrayItem("foto")]
    public List<Foto> Fotos { get; set; }
}

public class Foto
{
    [XmlText]
    public string data{ get; set; }
}
MorgoZ
  • 2,012
  • 5
  • 27
  • 54

1 Answers1

0

When using XmlElement for list-types the container-list-element within the resulting xml is lost. As you do have that container for your fotos-tag, you should use XmlArrayItem for it:

public class Portfolio
{
    [XmlArray("fotos")]
    [XmlArrayItem("foto")]
    public List<Foto> Fotos { get; set; }
}

If you can change the xml however I would suggest to use a consistent style for your collections, either use the container for your portfolio also, or omit it for your fotos.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Thanks for your reply. I'm not sure to understand what you mean by using a container for the "portfolio". I've updated the XML so you can see that there are many "portfolios" inside one "mainfile". In any case, your solution is still not working for me, i'm still getting an empty list of "fotos". – MorgoZ Feb 25 '19 at 08:55
  • @MorgoZ By container I mean there is a further nested foto-tag within fotos-tag, in contrast to the portfolio-tag which is not surrounded by a portfolios – MakePeaceGreatAgain Feb 25 '19 at 09:03
  • @MorgoZ Maybe the problem is related to the cdata-section? What happens when you replace that text with something like "MyString"? – MakePeaceGreatAgain Feb 25 '19 at 09:40
  • Yes, you are right! That's the problem. Removing the CDATA tag returns the text properly. But how can a i manage to get the text from within the CDATA? As i checked in this stackoverflow question: https://stackoverflow.com/questions/397085/net-xmlserializer-deserialize-cdata-being-inner-text it should be transparent to use a CDATA or not... – MorgoZ Feb 25 '19 at 10:15
  • Ok, forget the last. Now is working perfect... With and without CDATA... Don't lnow what did i do wrong. Thank you. – MorgoZ Feb 25 '19 at 10:36
  • @MorgoZ Feel free to post your own answer. – MakePeaceGreatAgain Feb 25 '19 at 10:39