Invaild施放异常不确定原因 [英] Invaild cast exception not sure why

查看:144
本文介绍了Invaild施放异常不确定原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试读取并将xml文件存储到我的数据库时遇到问题

它给我一个无效的强制转换异常的错误

我是否错误地投了?如果是这样我该怎么做呢?

这是我读取xml文件的代码

 ArrayList inlist =  new  ArrayList(); 
foreach (节点列表中的XmlNode节点
{
OrderDetail od = new OrderDetail();
if (Node.Attributes.Count > 0
{
textBox2.Text + = Item_ID = + Node.Attributes [ 0 ]。Value + Environment.NewLine;
od.ItemId = Node.Attributes [ 0 ]。值;
}
if (Node.HasChildNodes)
{
XmlElement ChildNode =(XmlElement)Node.FirstChild;
for int i = 0 ; i < Node.ChildNodes.Count; i ++)
{
switch (ChildNode.Name)
{
case size
textBox2.Text + = Size = ;
od.Size = ChildNode.InnerText;
break ;
case color
textBox2.Text + = Color =;
od.Color = ChildNode.InnerText;
break ;
case quantity
textBox2.Text + = Quantity =;
od.Quantity = Convert.ToInt32(ChildNode.InnerText);
break ;
case askPrice
textBox2.Text + = ePrice =;
od.Askingprice = Convert.ToDecimal(ChildNode.InnerText);
break ;
case description
textBox2.Text + = desscription =;
od.Description = ChildNode.InnerText;
break ;
默认
break ;
}

textBox2.Text + = ChildNode.InnerText + = Environment.NewLine;
ChildNode =(XmlElement)ChildNode.NextSibling;
}
}
od.OrderId = o.orderId;
inlist.Add(od);
}





这是我想要阅读的xml文件

 <?  xml    版本  =  1.0   编码  =  utf-8  >  
<! - 本文档包含订购信息 - >
< OrderList xmlns = http://www.MyCompany.com > ;
< 订单 Order_ID = 1 Order_Date = 1/2/2013 Buyer_ID = GFS1810 >
< 备注 / >
< 项目 Item_ID = 123 尺寸 = M 颜色 = blue 数量 = 10 ePrice = < span class =code-keyword> 33.77 > 非常好< < span class =code-leadattribute> / Item >
< 项目 Item_ID = 23423423 大小 = M 颜色 = 红色 数量 = 10 ePrice = 44.10 > 请穿< / Item >
< 项目 Item_ID = 3242 大小 = XL 颜色 = green 数量 = 10 ePrice = 67.00 > 可以穿< / Item >
< 项目 Item_ID = 3456 大小 = S 颜色 = 粉红色 数量 = 10 ePrice = 10.00 > SoSo < /商品 >
< /订单 >
< 订单 Order_ID = 1 Order_Date = 1/2/2013​​ Buyer_ID = GFS1810 >
< 备注 / >
< 项目 Item_ID = 4535 大小 = L 颜色 = 黄色 数量 = 10 ePrice = 22.00 > 非常好< / Item >
< /订单 >
< / OrderList >

解决方案

Node.FirstChild 返回 XmlNode [ ^ ]。






理查德是对的。要将 XmlNode 转换为 XmlElement ,请尝试以下方法:

 XmlElement ChildNode = Node.FirstChild  as  XmlElement; 
if (ChildNode!= null / / 如果ChildNode不为null,则XmlNode成功转换为XmlElement
{
...
}



如果 ChildNode null ,那么 Node.FirstChild.NodeType 不是 XmlNodeType.Element ,在这种情况下 XmlNode 无法转换为 XmlElement



希望这会有所帮助。


I having problem while trying to read and store an xml file into my database
it give me an error of invalid cast exception
Do i cast wrongly?? If so how do i cast it?
this is my code that read the xml file

ArrayList inlist = new ArrayList();
            foreach (XmlNode Node in nodelist)
            {
                OrderDetail od = new OrderDetail();
                if (Node.Attributes.Count > 0)
                {
                    textBox2.Text += "Item_ID = " + Node.Attributes[0].Value + Environment.NewLine;
                    od.ItemId = Node.Attributes[0].Value;
                }
                if (Node.HasChildNodes)
                {
                    XmlElement ChildNode = (XmlElement)Node.FirstChild;
                    for (int i = 0; i < Node.ChildNodes.Count; i++)
                    {
                        switch (ChildNode.Name)
                        {
                            case "size":
                                textBox2.Text += "Size = ";
                                od.Size = ChildNode.InnerText;
                                break;
                            case "color":
                                textBox2.Text += "Color = ";
                                od.Color = ChildNode.InnerText;
                                break;
                            case "quantity":
                                textBox2.Text += "Quantity =";
                                od.Quantity = Convert.ToInt32(ChildNode.InnerText);
                                break;
                            case "askingPrice":
                                textBox2.Text += "ePrice =";
                                od.Askingprice = Convert.ToDecimal(ChildNode.InnerText);
                                break;
                            case "description":
                                textBox2.Text += "desscription =";
                                od.Description = ChildNode.InnerText;
                                break;
                            default:
                                break;
                        }

                        textBox2.Text += ChildNode.InnerText += Environment.NewLine;
                        ChildNode = (XmlElement)ChildNode.NextSibling;
                    }
                }
                od.OrderId = o.orderId;
                inlist.Add(od);
            }



this is my xml file that it suppose to read

<?xml version="1.0" encoding="utf-8"?>
<!--This document contains Ordering Information-->
<OrderList xmlns="http://www.MyCompany.com">
  <Order Order_ID="1" Order_Date="1/2/2013" Buyer_ID="GFS1810">
    <Remark />
    <Item Item_ID="123" Size="M" Color="blue" Quantities="10" ePrice="33.77">Very Nice</Item>
    <Item Item_ID="23423423" Size="M" Color="red" Quantities="10" ePrice="44.10">Please Wear</Item>
    <Item Item_ID="3242" Size="XL" Color="green" Quantities="10" ePrice="67.00">Can Wear</Item>
    <Item Item_ID="3456" Size="S" Color="pink" Quantities="10" ePrice="10.00">SoSo</Item>
  </Order>
  <Order Order_ID="1" Order_Date="1/2/2013" Buyer_ID="GFS1810">
    <Remark />
    <Item Item_ID="4535" Size="L" Color="yellow" Quantities="10" ePrice="22.00">Very Ok</Item>
  </Order>
</OrderList>

解决方案

Node.FirstChild returns an XmlNode[^].


Hi,

Richard is right. To cast the XmlNode to an XmlElement, try this:

XmlElement ChildNode = Node.FirstChild as XmlElement;
if (ChildNode != null) // if ChildNode isn't null, the XmlNode is successfully converted to an XmlElement
{
   ...
}


If ChildNode is null, then Node.FirstChild.NodeType isn''t XmlNodeType.Element, and in that case the XmlNode can''t be converted to an XmlElement.

Hope this helps.


这篇关于Invaild施放异常不确定原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆