1
以下網址:Deserialize xml into super class object with C#反序列化XML元素POCO基類
我已經實現:
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序列化的定義:
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
,divide
到其基類Calculator
。 xPlugin
的calculator
屬性總是得到空值。那麼我怎樣才能將這些元素反序列化到它的基類?
仍然得到了空值,先生! –
我得到了一個這樣的異常:http://i244.photobucket.com/albums/gg20/trongcuong1710/error-1.png。不知道問題的原因是什麼 –
它適用於我,你確定你沒有重複XmlArrayItem.I當我這樣做時有同樣的錯誤: [XmlArrayItem(「Multiple」,typeof(Multiple))] [XmlArrayItem(「Multiple」,typeof(Divide))] – fcuesta