我有一个嵌套的C#对象模型,我用XML表示。我希望能够从我的测试工具的文件中读取这个以创建测试对象,但是反序列化返回一个具有空值的对象,即零,空值。没有错误。
这个相同的代码块适用于我拥有的其他对象模型和XML文件,因此我似乎将XML映射到我的类结构。
有什么想法吗?
代码
private static PosMenu LoadPosMenu(XmlNode node)
{
var deserializer = new XmlSerializer(typeof(PosMenu));
using (var reader = new StringReader(node.OuterXml))
{
var item = deserializer.Deserialize(reader) as PosMenu;
if (item == null) throw new InvalidDataException("Could not load Pos menu from Xml");
return item;
}
}
对象模型
[DataContract]
public class PosMenu
{
[DataMember]
public int VenueId { get; set; }
[DataMember]
public int count { get; set; }
[DataMember]
public PosMenuEmbedded _embedded { get; set; }
}
[DataContract]
public class PosMenuEmbedded : IEntityComparable<PosMenuEmbedded>
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public PosMenuCategory[] categories { get; set; }
[DataMember]
public int PosMenuId { get; set; }
}
[DataContract]
public class PosMenuCategory
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public int VenueId { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public int PosMenuId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public PosMenuItem[] Items { get; set; }
}
[DataContract]
public class PosMenuItem
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public long PosMenuCategoryId { get; set; }
[DataMember]
public bool In_Stock { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public bool Open { get; set; }
[DataMember]
public string Pos_Id { get; set; }
[DataMember]
public double Price { get; set; }
[DataMember]
public PosPriceLevel[] Price_Levels { get; set; }
}
[DataContract]
public class PosPriceLevel
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public double Price { get; set; }
}
XML
<PosMenu>
<Count>3</Count>
<Categories>
<PosMenuCategory>
<Id>AdiRjiAp</Id>
<Name>Drinks</Name>
<Items>
<PosMenuItem>
<Id>gki84ia9</Id>
<In_Stock>true</In_Stock>
<Name>Soda</Name>
<Open>false</Open>
<Pos_Id>gki84ia9</Pos_Id>
<Price>150</Price>
<Price_Levels>
<PosPriceLevel>
<Id>Byineidy</Id>
<Price>150</Price>
</PosPriceLevel>
<PosPriceLevel>
<Id>g4T4dTBj</Id>
<Price>200</Price>
</PosPriceLevel>
<PosPriceLevel>
<Id>K6czkc8b</Id>
<Price>250</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
<PosMenuItem>
<Id>doTaLTyg</Id>
<In_Stock>true</In_Stock>
<Name>Orange Juice</Name>
<Open>false</Open>
<Pos_Id>doTaLTyg</Pos_Id>
<Price>175</Price>
<Price_Levels>
<PosPriceLevel>
<Id>L4iqKid8</Id>
<Price>175</Price>
</PosPriceLevel>
<PosPriceLevel>
<Id>K6T8MTzb</Id>
<Price>300</Price>
</PosPriceLevel>
<PosPriceLevel>
<Id>K6czkc8b</Id>
<Price>250</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
</Items>
</PosMenuCategory>
<PosMenuCategory>
<Id>AkT69Tgy</Id>
<Name>Appetizers</Name>
<Items>
<PosMenuItem>
<Id>GyiKLiLo</Id>
<In_Stock>false</In_Stock>
<Name>Chips and Salsa</Name>
<Open>false</Open>
<Pos_Id>GyiKLiLo</Pos_Id>
<Price>250</Price>
<Price_Levels>
<PosPriceLevel>
<Id>neiani8B</Id>
<Price>250</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
<PosMenuItem>
<Id>recb5cKX</Id>
<In_Stock>true</In_Stock>
<Name>Mozzarella Sticks</Name>
<Open>false</Open>
<Pos_Id>recb5cKX</Pos_Id>
<Price>425</Price>
<Price_Levels>
<PosPriceLevel>
<Id>Bycnrcdy</Id>
<Price>425</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
</Items>
</PosMenuCategory>
<PosMenuCategory>
<Id>rXcXyc7b</Id>
<Name>Burgers</Name>
<Items>
<PosMenuItem>
<Id>rMTAbTjr</Id>
<In_Stock>false</In_Stock>
<Name>New Bacon-ings</Name>
<Open>false</Open>
<Pos_Id>rMTAbTjr</Pos_Id>
<Price>689</Price>
<Price_Levels>
<PosPriceLevel>
<Id>7oTd8T9A</Id>
<Price>689</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
<PosMenuItem>
<Id>gkc8bca9</Id>
<In_Stock>false</In_Stock>
<Name>Poblana Picasso Burger</Name>
<Open>false</Open>
<Pos_Id>gkc8bca9</Pos_Id>
<Price>799</Price>
<Price_Levels>
<PosPriceLevel>
<Id>K6czEc8b</Id>
<Price>799</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
<PosMenuItem>
<Id>gdijnib8</Id>
<In_Stock>false</In_Stock>
<Name>Tunami</Name>
<Open>false</Open>
<Pos_Id>gdijnib8</Pos_Id>
<Price>799</Price>
<Price_Levels>
<PosPriceLevel>
<Id>L8ijGiGX</Id>
<Price>799</Price>
</PosPriceLevel>
</Price_Levels>
</PosMenuItem>
</Items>
</PosMenuCategory>
</Categories>
</PosMenu>
答案 0 :(得分:0)
重命名属性以使其类似于xml元素名称(并将xml中的“_embeded”元素重命名为“Embeded”):
[DataContract]
public class PosMenu
{
[DataMember]
public int VenueId { get; set; }
[DataMember]
public int Count { get; set; }
[DataMember]
public PosMenuEmbedded Embeded { get; set; }
}
[DataContract]
public class PosMenuEmbedded
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public PosMenuCategory[] Categories { get; set; }
[DataMember]
public int PosMenuId { get; set; }
}
作为替代解决方案,您可以将属性添加到属性中。例如:
[XmlElement(ElementName = "Count")]
public int count { get; set; }