XML反序列化为C#类时UOMS等部分元素无法正常加载
XML反序列化时UOMS对象无法填充的问题
尝试将XML反序列化为C#类时,大部分元素能正常加载,但XML中的UOMS对象始终无法被反序列化——无报错,但UOMS对象仅为默认值。试过在线XML转C#工具、[XmlArray]/[XmlArrayItem]特性均未解决问题。
示例XML
<Items xmlns="http://www.manh.com/ILSNET/Interface" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Item> <Desc>Desc Field Example Value</Desc> <Item>PE0000009790</Item> <ItemCategories> <Action>SAVE</Action> <Category1>COMPONENT</Category1> <Category2>Category2ExampleValue</Category2> </ItemCategories> <ItemClass> <Action>SAVE</Action> <ItemClass>Example ItemClass</ItemClass> </ItemClass> <UOMS> <UOM> <Action>SAVE</Action> <ConvQty>1</ConvQty> <DimensionUm>IN</DimensionUm> <Height>0.0</Height> <Length>0.0</Length> <QtyUm>EA</QtyUm> <Sequence>1</Sequence> <Weight>0</Weight> <WeightUm>LB</WeightUm> <Width>0.0</Width> </UOM> </UOMS> </Item> </Items>
现有代码
基础反序列化代码
using (FileStream fileStream = new FileStream(Filename, FileMode.Open)) { this.CreatedObjects = (ItemList)serializer.Deserialize(fileStream); }
第一部分类定义
[XmlRoot(ElementName = "Items", Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemList { [XmlElement("Item")] public Item[] Items { get; set; } } public class Item { [XmlElement("ItemCategories")] public ItemCategory[] Categories { get; set; } [XmlElement("ItemClass")] public ItemClass[] Classes { get; set; } [XmlElement("UOMS")] public ItemUOMS UOMs { get; set; } [XmlElement("Desc")] public string Description { get; set; } [XmlElement("Item")] public string Id { get; set; } }
无法填充的第二部分类定义
[XmlRoot(ElementName = "UOMS", Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOMS { [XmlElement("UOM")] public ItemUOM[] UOM { get; set; } } [XmlRoot(ElementName = "UOM", Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOM { [XmlElement("Action")] public string Action { get; } [XmlElement("DimensionUm")] public string DimensionUnit { get; } [XmlElement] public decimal Height { get; } [XmlElement] public decimal Length { get; } [XmlElement("QtyUm")] public string QtyUnit { get; } [XmlElement("ConvQty")] public decimal Quantity { get; } [XmlElement] public int Sequence { get; } [XmlElement] public decimal Weight { get; } [XmlElement("WeightUm")] public string WeightUnit { get; } [XmlElement] public decimal Width { get; } }
解决方案
问题出在三个关键点,逐一修复即可:
命名空间未统一
XML中所有元素都属于http://www.manh.com/ILSNET/Interface命名空间,但Item类中的UOMs属性未指定命名空间,导致反序列化器无法匹配元素。可以给Item类添加[XmlType]指定命名空间,确保所有嵌套类继承该命名空间:[XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class Item { // 原有属性代码... }或者给
UOMs属性的[XmlElement]显式指定命名空间:[XmlElement("UOMS", Namespace = "http://www.manh.com/ILSNET/Interface")] public ItemUOMS UOMs { get; set; }ItemUOM类属性只有getter
XML反序列化需要属性具备公共setter才能赋值,把ItemUOM中的所有只读属性修改为带setter的形式:[XmlElement("Action")] public string Action { get; set; } [XmlElement("DimensionUm")] public string DimensionUnit { get; set; } // 其他属性同理修改...[XmlRoot]特性误用
ItemUOMS和ItemUOM不是根元素,不需要[XmlRoot]特性,直接移除即可,改用[XmlType](可选,用于指定命名空间):[XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOMS { [XmlElement("UOM")] public ItemUOM[] UOM { get; set; } } [XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOM { // 修改后的属性代码... }
修复后的完整类代码示例
[XmlRoot(ElementName = "Items", Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemList { [XmlElement("Item")] public Item[] Items { get; set; } } [XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class Item { [XmlElement("ItemCategories")] public ItemCategory[] Categories { get; set; } [XmlElement("ItemClass")] public ItemClass[] Classes { get; set; } [XmlElement("UOMS")] public ItemUOMS UOMs { get; set; } [XmlElement("Desc")] public string Description { get; set; } [XmlElement("Item")] public string Id { get; set; } } [XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOMS { [XmlElement("UOM")] public ItemUOM[] UOM { get; set; } } [XmlType(Namespace = "http://www.manh.com/ILSNET/Interface")] public class ItemUOM { [XmlElement("Action")] public string Action { get; set; } [XmlElement("DimensionUm")] public string DimensionUnit { get; set; } [XmlElement("Height")] public decimal Height { get; set; } [XmlElement("Length")] public decimal Length { get; set; } [XmlElement("QtyUm")] public string QtyUnit { get; set; } [XmlElement("ConvQty")] public decimal Quantity { get; set; } [XmlElement("Sequence")] public int Sequence { get; set; } [XmlElement("Weight")] public decimal Weight { get; set; } [XmlElement("WeightUm")] public string WeightUnit { get; set; } [XmlElement("Width")] public decimal Width { get; set; } }
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

