2013-06-26 178 views
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(); 
     } 
    } 

反序列化過程中成功反序列化xPluginsxPlugin到其關聯的類,但未能反序列化addmultipledividedivide到其基類CalculatorxPlugincalculator屬性總是得到空值。那麼我怎樣才能將這些元素反序列化到它的基類?

回答

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; } 
} 
+0

仍然得到了空值,先生! –

+0

我得到了一個這樣的異常:http://i244.photobucket.com/albums/gg20/trongcuong1710/error-1.png。不知道問題的原因是什麼 –

+0

它適用於我,你確定你沒有重複XmlArrayItem.I當我這樣做時有同樣的錯誤: [XmlArrayItem(「Multiple」,typeof(Multiple))] [XmlArrayItem(「Multiple」,typeof(Divide))] – fcuesta