到目前为止,我有以下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; }
}
我的Xml看起来像这样
<stat directory="/dir1/dir2">
<file type="file" name="file.html" mtime="1260000000" size="1234567"
md5="0123456789abcdef0123456789abcdef" />
<file type="dir" name="dir3" mtime="1260000000" />
</stat>
我想要实现的是一个足够聪明的结构,可以将不同类型的文件节点序列化为不同的对象,所以
[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; }
...
}
那么我可以根据属性将元素定向到填充不同的对象吗?我知道我可以用xsi:type
来做