我正在尝试将xml文档反序列化为C#类。这是文件。
<?xml version="1.0" encoding="utf-8"?>
<Exclusionpolicys>
<Exclusionpolicy>
<ValuationRoutes>
<ValuationRoute>value1</ValuationRoute>
<ValuationRoute>Value2</ValuationRoute>
</ValuationRoutes>
<ExcludeHives>
<ExcludeHive name="samplename1"/>
<ExcludeHive name="samplename2"/>
<ExcludeHive name="samplename3"/>
<ExcludeHive name="samplename4"/>
</ExcludeHives>
</Exclusionpolicy>
<Exclusionpolicy>
<ValuationRoutes>
<ValuationRoute>Value3</ValuationRoute>
</ValuationRoutes>
<ExcludeHives>
<ExcludeHive name="samplename5"/>
</ExcludeHives>
</Exclusionpolicy>
</Exclusionpolicys>
这是我创建的课程。
[XmlRootAttribute("Exclusionpolicys")]
public class ExclusionPolicys
{
[XmlElement("ExcludeHive ")]
public ExcludedPolicyHives[] ExcludedHives { get; set; }
[XmlElement("ValuationRoute")]
public ExcludedPolicyValuationRoute[] ValuationRoutes { get; set; }
}
public class ExcludedPolicyHives : IExcludedHives
{
public List<string> Hives { get; set; }
}
public class ExcludedPolicyValuationRoute
{
public List<string> ValuationRoutes { get; set; }
}
当我反序列化它时...它不会抛出任何错误,但是给我一个ExclusionPolicys的对象,这两个属性都是null值。
任何人都可以帮助确定我的班级定义有什么问题。
我想将它反序列化为keyValue对列表/数组或与之类似。
提前致谢。
感谢您的帮助。工作副本在这里......
[XmlRoot("Exclusionpolicys")]
public class ExclusionPolicys
{
[XmlElement("Exclusionpolicy")]
public List<Exclusionpolicy> Exclusionpolicy { get; set; }
}
public class Exclusionpolicy
{
[XmlElement("ValuationRoutes")]
public List<ExcludedPolicyValuationRoutes> ValuationRoutes { get; set; }
[XmlElement("ExcludeHives")]
public List<ExcludedHiveses> ExcludedHives { get; set; }
}
[XmlRoot("ExcludeHives")]
public class ExcludedHiveses
{
[XmlElement(ElementName = "ExcludeHive")]
public List<string> Hives { get; set; }
}
public class ExcludedPolicyValuationRoutes
{
[XmlElement("ValuationRoute")]
public List<string> ValuationRoute { get; set; }
}
答案 0 :(得分:0)
将类库更改为以下代码,因为XML Deserialixed必须与XML一致,即:类库必须直接反映XML代码。
[XmlRoot("Exclusionpolicys")]
public class ExclusionPolicys
{
[XmlElement("Exclusionpolicy")]
public List<Exclusionpolicy> Exclusionpolicy;
}
public class Exclusionpolicy
{
[XmlElement("ValuationRoutes")]
public List<ExcludedPolicyValuationRoutes> ValuationRoutes { get; set; }
[XmlElement("ExcludeHives")]
public List<ExcludedPolicyHives> ExcludedHives { get; set; }
}
public class ExcludedPolicyHives
{
[XmlElement("ExcludeHive")]
public List<ExcludedPolicyHive> ExcludedPolicyHive;
}
public class ExcludedPolicyValuationRoutes
{
[XmlElement("ValuationRoute")]
public List<ValuationRoute> ValuationRoute { get; set; }
}
public class ExcludedPolicyHives
{
public string Hives { get; set; }
}
public class ExcludedPolicyValuationRoute
{
public string ValuationRoutes { get; set; }
}
答案 1 :(得分:0)
在Visual Studio 2012中有一个转换工具 - 我不知道它与其他VS的外观。
现在您已经生成了类,您不必手动创建它。