我正在处理一项需要从会话创建 XML 文件的任务。我有一个铸造问题,我知道它来自列表,这是存储购物车中的物品的地方,但我不知道如何解决它。
按钮单击是一个创建 XML 按钮,这是我得到的确切错误:
Unable to cast object of type Utils.ShoppingCart' to type 'System.Collections.Generic.List
1 [CartItem]'。
Line 86: List<CartItem> cartItems = new List<CartItem>();
Line 87: cartItems = (List<CartItem>)Session["UserCart"];
这是代码。如果我需要提供更多信息,请告诉我。我将不胜感激任何反馈!
protected void Button1_Click(object sender, EventArgs e)
{
ShoppingCart sCart = (ShoppingCart)Session["UserCart"];
List<CartItem> cartItems = new List<CartItem>();
cartItems = (List<CartItem>)Session["UserCart"];
XmlWriterSettings sets = new XmlWriterSettings();
sets.Indent = true;
using (XmlWriter writer = XmlWriter.Create(Server.MapPath("~/items.xml"), sets))
{
writer.WriteStartDocument();
writer.WriteStartElement("cartItems");
foreach (CartItem i in cartItems)
{
writer.WriteStartElement("CartItem");
writer.WriteElementString("ITEM_ID", i.ITEM_ID.ToString());
writer.WriteElementString("ITEM_QUANTITY", i.ITEM_QUANTITY.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
writer.Close();
Label1.Text = "XML written successfully!";
Label2.Text = "<a href='DOM/cart.xml'> XML created successfully. </a>";
}
}