【发布时间】:2012-04-13 23:29:39
【问题描述】:
我真的很难将以下 XML 反序列化为 C# 对象;
<docRoot>
...
<doc-sets>
<docs>
<atom:link rel="related" href="http://blah.com/1" title="abc" xmlns:atom="http://www.w3.org/2005/Atom" />
<atom:link rel="related" href="http://blah.com/2" title="abc2" xmlns:atom="http://www.w3.org/2005/Atom" />
</docs>
<docs>
<atom:link rel="related" href="http://blah.com/1" title="abc" xmlns:atom="http://www.w3.org/2005/Atom" />
<atom:link rel="related" href="http://blah.com/2" title="abc2" xmlns:atom="http://www.w3.org/2005/Atom" />
</docs>
....
</doc-sets>
</docRoot
我发现之前的问题非常相似 (Deserialize List<ArrayList> object),但我也遇到了与原始发帖人相同的问题。
我可以创建一个对象,其中包含所有链接的组合列表,但我想保持有 2 个“文档”元素的事实,并将两个链接分开。
到目前为止我的代码;
[XmlRoot("docRoot")]
public class DocRoot
{
[XmlElement("doc-sets")]
public List<Docs> DocSets;
}
public class Link
{
[XmlAttribute("href")]
public string Href;
}
public class Docs
{
[XmlArray("docs")]
[XmlArrayItem("link", typeof(Link), Form = XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/2005/Atom")]
public List<Link> Links;
public Docs()
{
Links = new List<Link>();
}
}
任何想法我如何保留包含自己的链接而不是一个组合的链接列表的 2 个“文档”元素?
谢谢
【问题讨论】:
标签: c# xml deserialization