2012-08-23 70 views
1

你好我有以下反串行化代碼部分未正確反序列化

 public static T DeserializeXML<T>(String xml) where T : class 
     { 
      T newObject = null; 
      XmlSerializer s = new XmlSerializer(typeof(T)); 
      using (StringReader sw = new StringReader(xml)) 
      { 
       newObject = (T)s.Deserialize(sw); 
      } 
      return newObject; 
     } 

我的消息,我嘗試反序列化

<Data> 
<ItemIn date="2012-08-09T10:25:54.06+01:00" itemId="000007721" Id="1"> <Extensions><Info Id="parts" order="issue"/></Extensions></ItemIn> 
</Data> 

但我從來沒有得到Extensions部分desirialized回到原來的我總是在那裏得到null。其他的課程還可以。

任何建議要檢查什麼?

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
public partial class ItemTransferIn { 

    private Extensions extensions; 

    private System.DateTime date; 

    private string itemId; 

    private string Id; 

    /// <remarks/> 
    public ItemTransferInExtensions Extensions { 
     get { 
      return this.extensions; 
     } 
     set { 
      this.extensions = value; 
     } 
    } 

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class Extensions { 

    private RecipeInfo recipeInfoField; 

    /// <remarks/> 
    public RecipeInfo RecipeInfo { 
     get { 
      return this.recipeInfoField; 
     } 
     set { 
      this.recipeInfoField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
public partial class RecipeInfo { 

    private string recipeIdField; 

    private string orderIdField; 

    private string itemBarcodeIdField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Id { 
     get { 
      return this.recipeIdField; 
     } 
     set { 
      this.recipeIdField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string order { 
     get { 
      return this.orderIdField; 
     } 
     set { 
      this.orderIdField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string itemBarcodeId { 
     get { 
      return this.itemBarcodeIdField; 
     } 
     set { 
      this.itemBarcodeIdField = value; 
     } 
    } 
} 
+0

那麼,你的DTO課程是什麼樣的? –

+0

Marc Gravell♦見我的編輯 –

回答

2

對於擴展......看起來您已經編輯了代碼;有

private Extensions extensions; 

... 

/// <remarks/> 
public ItemTransferInExtensions Extensions { 
    get { 
     return this.extensions; 
    } 
    set { 
     this.extensions = value; 
    } 
} 

坦率地說,那甚至不應該編譯;我們不ItemTransferInExtensions類。

此外,Info將無法​​正常工作:

/// <remarks/> 
public RecipeInfo RecipeInfo {...blah...} 

不匹配<Info ..../>。所以要麼更正xsd並重新生成cs,要麼更正xml;但目前它們不匹配

RecipeInfo屬性重命名爲Info(你也可以只添加屬性),並固定擴展/ ItemTransferInExtensions(和失蹤}),並添加根類相匹配的XML後:

public class Data 
{ 
    public ItemTransferIn ItemIn { get; set; } 
} 

......這一切工作正常:

static void Main() 
{ 
    string msg = @"<Data> 
<ItemIn date=""2012-08-09T10:25:54.06+01:00"" itemId=""000007721"" Id=""1""> <Extensions><Info Id=""parts"" order=""issue""/></Extensions></ItemIn> 
</Data>"; 
    var obj = DeserializeXML<Data>(msg); 

    Console.WriteLine(obj.ItemIn.Extensions.Info.order); // issue 
} 

坦率地說,雖然,這是比較容易做手工:

public class Data 
{ 
    public ItemTransferIn ItemIn { get; set; } 
} 
public class ItemTransferIn 
{ 
    [XmlAttribute("date")] 
    public DateTime Date { get; set; } 

    [XmlAttribute("itemId")] 
    public string Itemid { get; set; } 

    [XmlAttribute] 
    public int Id { get; set; } 

    public Extensions Extensions { get; set; } 
} 
public class Extensions 
{ 
    public ExtensionsInfo Info { get; set; } 
} 
public class ExtensionsInfo 
{ 
    public int Id { get; set; } 
    [XmlAttribute("order")] 
    public string Order { get; set; } 
}