2016-01-18 49 views
0

我有下面的C#類不能夠反序列化XML轉換成通用的清單

[XmlTypeAttribute(AnonymousType = true)] 
public class TransactionTypesHolder 
{ 
    [XmlArrayItem("TransactionType", typeof(TransactionType))] 
    public List<TransactionType> TransactionTypes { get; set; } 

    public TransactionTypesHolder() 
    { 
     TransactionTypes = new List<TransactionType>(); 
    } 
} 


public class TransactionType 
{ 
    [XmlElement("Mti")] 
    public string Mti { get; set; } 

    [XmlElement("Description")] 
    public string Description { get; set; } 

    //[XmlArrayItem("Field", typeof(Field))] 
    //public List<Field> Fields { get; set; } 

    [XmlArray(ElementName = "Fields")] 
    [XmlArrayItem("Field", typeof(Field))] 
    public List<Field> Fields { get; set; } 

} 

[Serializable] 
public class Field 
{ 
    [XmlElement("FieldNumber")] 
    public string FieldNumber { get; set; } 

    [XmlElement("Description")] 
    public string Description { get; set; } 

    //[XmlArrayItem("FieldPart", typeof(FieldPart))] 
    //public List<FieldPart> FieldParts { get; set; } 

    [XmlArray(ElementName = "FieldParts")] 
    [XmlArrayItem("FieldPart", typeof(FieldPart))] 
    public List<FieldPart> FieldParts { get; set; } 
} 

[Serializable] 
public class FieldPart 
{ 
    [XmlElement("PartNumber")] 
    public string PartNumber { get; set; } 

    [XmlElement("PartValue")] 
    public string PartValue { get; set; } 
} 

我試圖反序列化通過下面的代碼

try 
      { 
       XmlSerializer serializer = new 
        XmlSerializer(typeof(TransactionTypesHolder)); 

       // A FileStream is needed to read the XML document. 
       FileStream fs = new FileStream("Xmlfile1.xml", FileMode.Open); 
       XmlReader reader = XmlReader.Create(fs); 

       // Declare an object variable of the type to be deserialized. 
       TransactionTypesHolder i; 

       // Use the Deserialize method to restore the object's state. 
       i = (TransactionTypesHolder)serializer.Deserialize(reader); 
       fs.Close(); 

      } 
      catch (Exception ex) 
      { 


      } 

但得到錯誤是沒有預料到。

我的XML是

<?xml version="1.0" ?> 
<TransactionTypes> 
    <TransactionType> 
    <Mti>200</Mti> 
    <Description>Debit</Description> 
    <Fields> 
     <Field> 
     <FieldNumber>3</FieldNumber> 
     <Description></Description> 
     <FieldParts> 
      <FieldPart> 
      <PartNumber> 
       1 
      </PartNumber> 
      <PartValue> 
       00 
      </PartValue> 
      <Description></Description> 
      </FieldPart> 
      <FieldPart> 
      <PartNumber> 
       2 
      </PartNumber> 
      <PartValue> 
       00 
      </PartValue> 
      <Description></Description> 
      </FieldPart> 
     </FieldParts> 
     </Field> 
     <Field> 
     <FieldNumber>14</FieldNumber> 
     <Description></Description> 
     <FieldParts> 
      <FieldPart> 
      <PartNumber> 
       1 
      </PartNumber> 
      <PartValue> 
       9911 
      </PartValue> 
      <Description></Description> 
      </FieldPart> 
     </FieldParts> 
     </Field> 
    </Fields> 
    </TransactionType> 
    <TransactionType> 
    <Mti>100</Mti> 
    <Description>Balance</Description> 
    <Fields> 
     <Field> 
     <FieldNumber>3</FieldNumber> 
     <Description></Description> 
     <FieldParts> 
      <FieldPart> 
      <PartNumber> 
       1 
      </PartNumber> 
      <PartValue> 
       31 
      </PartValue> 
      <Description></Description> 
      </FieldPart> 
      <FieldPart> 
      <PartNumber> 
       2 
      </PartNumber> 
      <PartValue> 
       00 
      </PartValue> 
      <Description></Description> 
      </FieldPart>   
     </Field> 
     <Field> 
     <FieldNumber>14</FieldNumber> 
     <Description></Description> 
     <FieldParts> 
      <FieldPart> 
      <PartNumber> 
       1 
      </PartNumber> 
      <PartValue> 
       9911 
      </PartValue> 
      <Description></Description> 
      </FieldPart> 
     </FieldParts> 
     </Field> 
    </Fields> 
    </TransactionType> 
</TransactionTypes> 

回答

0

我用下面的類

[XmlRoot("TransactionTypes")] 
    public class TransactionTypes 
    { 
     [XmlElement("TransactionType")] 
     public List<TransactionType> TransactionType { get; set; } 
    } 

,而不是TransactionTypesHolder併成功地能至 反序列化的xml

0

這看起來錯:

[XmlElement("TransactionTypes")] 
    public List<TransactionType> TransactionTypes { get; set; } 

嘗試改變:

[XmlArray(ElementName="TransactionTypes")] 
    [XmlArrayItem("TransactionType", typeof(TransactionType))] 
    public List<TransactionType> TransactionTypes { get; set; } 

而且改變:

[XmlArrayItem("Field", typeof(Field))] 
    public List<Field> Fields { get; set; } 

到:

[XmlArray(ElementName="Fields")] 
    [XmlArrayItem("Field", typeof(Field))] 
    public List<Field> Fields { get; set; } 

與同爲:

[XmlArrayItem("FieldPart", typeof(FieldPart))] 
    public List<FieldPart> FieldParts { get; set; } 

到:

[XmlArray(ElementName="FieldParts")] 
    [XmlArrayItem("FieldPart", typeof(FieldPart))] 
    public List<FieldPart> FieldParts { get; set; } 
+0

我已經相應地改變了它,但得到相同的錯誤不是預期的。 –

+0

任何想法我做錯了什麼? –

+0

更改字段和場部件的屬性後無效。事實上,我已經成功地將單個TransactionType xml條目反序列化爲TransactionType對象。問題只在列表填寫中 –