[英]Xml Deserialization - Sequence of Mutiple Types
Given the following fragment where links is a sequence of unbounded imagelinks and documentlinks, what should the deserailized class be? 给定以下片段,其中链接是无限制的图像链接和文档链接的序列,那么反序列化的类应该是什么?
<Values>
<Links>
<ImageLink>http://#</ImageLink>
<ImageLink>http://#</ImageLink>
<DocumentLink>http://#</DocumentLink>
</Links>
</Values>
Typically, if it was just an array of imagelinks I might have 通常,如果只是一个图像链接数组,我可能会有
public class Values
{
public imagelink[] ImageLinks { get; set; }
}
public class ImageLink
{
public string Value { get; set; }
}
But with the above xml I'm stumped. 但是上面的xml令我很困惑。
Btw, I have no control over the xml. 顺便说一句,我无法控制xml。
This worked 这工作
public class DocumentLink : Link
{
}
public class ImageLink : Link
{
}
public class Link
{
[XmlText]
public string Href { get; set; }
}
public class Values
{
[XmlArrayItem(ElementName = "ImageLink", Type = typeof(ImageLink))]
[XmlArrayItem(ElementName = "DocumentLink", Type = typeof(DocumentLink))]
public Link[] Links { get; set; }
}
You should have a base class Link as follows 您应该具有如下的基类链接
public class Link
{
public string Href { get; set; }
}
public class ImageLink : Link
{
}
public class DocumentLink : Link
{
}
And your values class would look like: 您的值类如下所示:
public class Values
{
public Link[] links { get; set; }
}
Alternatively, you could use ArrayList instead of strong typed array. 或者,您可以使用ArrayList代替强类型数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.