反序列化数组元素以列出
我有以下 XML......
<configuration>
<img name="name1" />
<img name="name2" />
<warn>
<img name="warn1" />
</warn>
</configuration>
我尝试将其反序列化为......
[XmlType("img")]
public class ImageNameExceptionItemXml
{
[XmlAttribute("name")]
public string Filename;
}
[XmlRoot("configuration")]
public class ImageNameExceptionListXml: List<ImageNameExceptionItemXml>
{
[XmlArray("warn")]
[XmlArrayItem("img")]
public ImageNameExceptionListXml WarnList { get; set; }
}
但最终得到 WarnList 属性为 null。
我已经尝试过...
[XmlElement("warn"}]
public ImageNameExceptionListXml WarnList { get; set; }
...或者...
[XmlElement("warn"}]
public List<ImageNameExceptionItemXml> WarnList { get; set; }
...但我仍然以 WarnList 属性为空。这是为什么?
I have the following XML...
<configuration>
<img name="name1" />
<img name="name2" />
<warn>
<img name="warn1" />
</warn>
</configuration>
...which I try to deserialize into...
[XmlType("img")]
public class ImageNameExceptionItemXml
{
[XmlAttribute("name")]
public string Filename;
}
[XmlRoot("configuration")]
public class ImageNameExceptionListXml: List<ImageNameExceptionItemXml>
{
[XmlArray("warn")]
[XmlArrayItem("img")]
public ImageNameExceptionListXml WarnList { get; set; }
}
...but I end up with the WarnList property null.
I already tried...
[XmlElement("warn"}]
public ImageNameExceptionListXml WarnList { get; set; }
...or...
[XmlElement("warn"}]
public List<ImageNameExceptionItemXml> WarnList { get; set; }
...but I still end up with WarnList property null. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们直奔问题吧。 warn 是一个“根”元素,因此,您也必须在类中对其进行转换:
xml:
类:
以及反序列化测试:
Let's go to the problem. The warn is a "root" element, so, you have to transform it in a class too:
The xml:
The class:
And the deserialize test:
XmlSerializer 只允许数组/列表中的一种类型。很好。发布我的测试代码:
xml:
XmlSerializer just allow one type in array/list. is fine. Post My test code:
xml: