[英]XML Deserialization results in empty object
I'm attempting to deserialize this XML file into an object 我正在尝试将此XML文件反序列化为一个对象
<?xml version="1.0" encoding="utf-8" ?>
<rules version="3">
<emie>
<domain>msdn.microsoft.com</domain>
<domain exclude="false">
bing.com
<path exclude="true">images</path>
</domain>
<domain exclude="true">
news.msn.com
<path exclude="false">pop-culture</path>
</domain>
<domain>timecard</domain>
<domain>tar</domain>
</emie>
</rules>
I have my objects laid out like so 我有这样布置的东西
[XmlRoot("rules")]
public class Rules {
[XmlAttribute("version")]
public string Version { get; set; }
[XmlElement("emie")]
public EMIE EMIE { get; set; }
}
public class EMIE {
[XmlArrayItem("Domain")]
public List<Domain> Domains { get; set; }
}
public class Domain {
[XmlAttribute("exclude")]
public bool Exclude { get; set; }
[XmlText]
public string Value { get; set; }
[XmlArrayItem("Path")]
public List<Path> Paths { get; set; }
}
public class Path {
[XmlAttribute]
public bool Exclude { get; set; }
[XmlText]
public string Value { get; set; }
}
And am using this code to deserialize it 并且正在使用此代码反序列化它
static void Main(string[] args) {
XmlSerializer serializer = new XmlSerializer(typeof(Rules));
using (FileStream stream = new FileStream("EM.xml", FileMode.Open)) {
Rules xml = (Rules)serializer.Deserialize(stream);
foreach (Domain d in xml.EMIE.Domains) {
Console.WriteLine(d.Value);
foreach (EnterpriseModeModel.Path p in d.Paths) {
Console.WriteLine(p.Value);
}
}
}
Console.ReadLine();
}
However, my rules.EMIE.Domains
object is always empty. 但是,我的rules.EMIE.Domains
对象始终为空。 When I debug I can see my stream
object has a length so it's properly picking up the data in the file but it never fills up the object like I expect it to. 当我调试时,我可以看到我的stream
对象有一个长度,因此它可以正确地拾取文件中的数据,但它永远不会像我期望的那样填满对象。
Change the declaration of EMIE
as follows 如下更改EMIE
的声明
public class EMIE
{
[XmlElement("domain")]
public List<Domain> Domains { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.