点击此链接:Deserialize xml into super class object with C#
我已实施:
xml文件
<?xml version="1.0" encoding="utf-8" ?>
<xPlugins>
<xPlugin>
<Multiple>
<Operator>
<Add>
<Operator>1</Operator>
<Operator>2</Operator>
</Add>
</Operator>
<Operator>
<Add>
<Operator>3</Operator>
<Operator>4</Operator>
</Add>
</Operator>
</Multiple>
</xPlugin>
</xPlugins>
类
public abstract class Calculator
{
public Calculator() { }
public virtual int Calculate()
{
return 0;
}
}
public class Operator : Calculator
{
public int value { get; set; }
public Operator() { }
public Operator(string name)
{
}
public override int Calculate()
{
return value;
}
}
public class Add : Calculator
{
public List<Calculator> calculators { get; set; }
public Add() { }
public Add(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
List<int> value = new List<int>();
foreach (Calculator calculator in calculators)
{
value.Add(calculator.Calculate());
}
return value.Sum();
}
}
public class Minus : Calculator
{
public List<Calculator> calculators { get; set; }
public Minus() { }
public Minus(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value -= calculators[i].Calculate();
}
return value;
}
}
public class divide: Calculator
{
public List<Calculator> calculators { get; set; }
public Divide() { }
public Divide(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value /= calculators[i].Calculate();
}
return value;
}
}
public class Multiple : Calculator
{
public List<Calculator> calculators { get; set; }
public Multiple() { }
public Multiple(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value *= calculators[i].Calculate();
}
return value;
}
}
public class xPlugin
{
public Calculator calculator { get; set; }
}
public class xPlugins
{
[XmlElement("xPlugin", typeof(xPlugin))]
public xPlugin[] Plugin { get; set; }
}
Xml Serializer定义:
public static void getObject(ref xPlugins plugins)
{
try
{
List<Type> type = new List<Type>();
type.Add(typeof(Add));
type.Add(typeof(Minus));
type.Add(typeof(Multiple));
type.Add(typeof(Subtract));
type.Add(typeof(Operator));
XmlSerializer xml = new XmlSerializer(typeof(xPlugins), type.ToArray());
FileStream fs = new FileStream("test.xml", FileMode.Open);
plugins = (xPlugins)xml.Deserialize(fs);
}
catch (Exception ex)
{
throw;
}
}
使用:
private void button1_Click(object sender, EventArgs e)
{
readXML.getObject(ref this.plugins);
foreach (xPlugin plugin in plugins.Plugin)
{
plugin.calculator.Calculate();
}
}
反序列化流程成功,将xPlugins
和xPlugin
反序列化到其关联类,但无法反序列化add
,multiple
,divide
,{{1}进入其基类divide
。 Calculator
的{{1}}属性始终为null值。那么我怎样才能将这些元素反序列化到它的基类呢?
答案 0 :(得分:1)
如果您可以将xml更改为:
<?xml version="1.0" encoding="utf-8" ?>
<xPlugins>
<xPlugin>
<Multiple>
<Operators>
<Add>
<Operators>
<Operator>
<value>1</value>
</Operator>
<Operator>
<value>2</value>
</Operator>
</Operators>
</Add>
<Add>
<Operators>
<Operator>
<value>3</value>
</Operator>
<Operator>
<value>4</value>
</Operator>
</Operators>
</Add>
</Operators>
</Multiple>
</xPlugin>
</xPlugins>
这应该有效:
public abstract class Calculator
{
public Calculator() { }
public virtual int Calculate()
{
return 0;
}
}
public class Operator : Calculator
{
public int value { get; set; }
public Operator() { }
public Operator(string name)
{
}
public override int Calculate()
{
return value;
}
}
public class Add : Calculator
{
[XmlArray("Operators")]
[XmlArrayItem("Add", typeof(Add))]
[XmlArrayItem("Minus", typeof(Minus))]
[XmlArrayItem("Multiple", typeof(Multiple))]
[XmlArrayItem("Operator", typeof(Operator))]
public List<Calculator> calculators { get; set; }
public Add() { }
public Add(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
List<int> value = new List<int>();
foreach (Calculator calculator in calculators)
{
value.Add(calculator.Calculate());
}
return value.Sum();
}
}
public class Minus : Calculator
{
[XmlArray("Operators")]
[XmlArrayItem("Add", typeof(Add))]
[XmlArrayItem("Minus", typeof(Minus))]
[XmlArrayItem("Multiple", typeof(Multiple))]
[XmlArrayItem("Operator", typeof(Operator))]
public List<Calculator> calculators { get; set; }
public Minus() { }
public Minus(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value -= calculators[i].Calculate();
}
return value;
}
}
public class Divide : Calculator
{
[XmlArray("Operators")]
[XmlArrayItem("Add", typeof(Add))]
[XmlArrayItem("Minus", typeof(Minus))]
[XmlArrayItem("Multiple", typeof(Multiple))]
[XmlArrayItem("Operator", typeof(Operator))]
public List<Calculator> calculators { get; set; }
public Divide() { }
public Divide(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value /= calculators[i].Calculate();
}
return value;
}
}
public class Multiple : Calculator
{
[XmlArray("Operators")]
[XmlArrayItem("Add", typeof(Add))]
[XmlArrayItem("Minus", typeof(Minus))]
[XmlArrayItem("Multiple", typeof(Multiple))]
[XmlArrayItem("Operator", typeof(Operator))]
public List<Calculator> calculators { get; set; }
public Multiple() { }
public Multiple(List<Calculator> calculators)
{
this.calculators = calculators;
}
public override int Calculate()
{
int value = calculators[0].Calculate();
for (int i = 1; i < calculators.Count; i++)
{
value *= calculators[i].Calculate();
}
return value;
}
}
public class xPlugin
{
[XmlElement("Add", typeof(Add))]
[XmlElement("Minus", typeof(Minus))]
[XmlElement("Multiple", typeof(Multiple))]
[XmlElement("Operator", typeof(Operator))]
public Calculator calculator { get; set; }
}
public class xPlugins
{
[XmlElement("xPlugin", typeof(xPlugin))]
public xPlugin[] Plugin { get; set; }
}