问题描述
请教以下的XML文件使用Xml.Serialization如何序列化,<?xmlversion="1.0"encoding="utf-8"?><shoppingListxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><itemname="eggs"price="1.49"/><itemname="groundbeef"price="3.69"/><itemname="bread"price="0.89"/><itemname="caffeine"price="0.99"/></shoppingList>ShoppingList类应该如何建?我希望用以下的代码实现XmlSerializerserializer=newXmlSerializer(typeof(ShoppingList));FileStreamreader=newFileStream("TEST.XML",FileMode.Open);setting=(ShoppingList)serializer.Deserialize(reader);reader.Close();问题在于<shoppingList></shoppingList>之间不存在<items>这样的标签
解决方案
解决方案二:
[Serializable]publicclassShoppingList{[XmlElement("item")]publicItem[]item;}[Serializable]publicclassItem{[XmlAttribute("name")]publicstringname;[XmlAttribute("price")]publicstringprice;}这样应该就可以了