0

So far I have the following PCO

[XmlRoot("stat"), Serializable]
public class AkamaiDirectoryStructure
{
    [XmlAttribute("directory")]
    public string DirectoryName { get; set; }

    [XmlElement("file")]
    public AkamaiFileStructure Files { get; set; }
}

public class AkamaiFileStructure
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAttribute("mtime")]
    public long MTime { get; set; }

    [XmlAttribute("target")]
    public string Target { get; set; }

    [XmlAttribute("size")]
    public long Size { get; set; } 
    [XmlAttribute("md5")]
    public string MD5 { get; set; }
}

My Xml looks like this

<stat directory="/dir1/dir2">
    <file type="file" name="file.html" mtime="1260000000" size="1234567"
    md5="0123456789abcdef0123456789abcdef" />
    <file type="dir" name="dir3" mtime="1260000000" />
</stat>

What I would like to achieve is a structure smart enough to serialize the different types of file nodes into different objects so

[XmlRoot("stat"), Serializable]
public class AkamaiDirectoryStructure
{
    [XmlAttribute("directory")]
    public string DirectoryName { get; set; }

    [XmlElement("file")] // where type = file 
    public AkamaiFileStructure Files { get; set; }

   [XmlElement("file")] // where type = dir
   public AkamaiSubDirectoryStructure SubDirectories { get; set; }

   ...
}

So can I direct an element to a populate a different object based on an attribute? I know I can do it with the xsi:type

Mike
  • 5,918
  • 9
  • 57
  • 94
  • does `file type="dir"` also have children? – andrei.ciprian Aug 24 '16 at 13:46
  • Your desired code has two properties named `Files` that is not allowed. – Chris Dunaway Aug 24 '16 at 13:46
  • @andrei.ciprian no right now but you can see that they have different other attributes (size Md5, target) but that is one reason I may want to differentiate is because it may have children in the future – Mike Aug 24 '16 at 13:55
  • @ChrisDunaway thank you I fixed the example – Mike Aug 24 '16 at 13:56
  • Look at [this](http://stackoverflow.com/questions/17236823/can-i-provide-custom-serialization-for-xmlserializer-without-implementing-ixmlse) question's accepted answer. – galenus Aug 24 '16 at 14:26
  • @galenus review it, I do not see how it answers my question. Thanks for looking it up though. – Mike Sep 04 '16 at 21:41
  • @Mike you'd like to deserialize the same xml string into different objects based on the 'type' attribute, right? Well, there is no built-in way, but the answer I linked describes how you could achieve this using a custom serializer. – galenus Sep 05 '16 at 15:40

0 Answers0