473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

not sure why invalid cast exception

6
I having problem trying to read from xml file and store it in to my database

Expand|Select|Wrap|Line Numbers
  1. nodelist = xmlDoc.GetElementsByTagName("Item");
  2. ArrayList inlist = new ArrayList();
  3. foreach (XmlNode Node in nodelist)
  4.  {
  5.  OrderDetail od = new OrderDetail();
  6.  if (Node.Attributes.Count > 0)
  7.  {
  8.  textBox2.Text += "Item_ID = " + Node.Attributes[0].Value + Environment.NewLine;
  9. od.ItemId = Node.Attributes[0].Value;
  10. }
  11.  if (Node.HasChildNodes)
  12. {
  13.  XmlElement ChildNode = (XmlElement)Node.FirstChil
  14. (the error is here which is in Bold)
  15. for (int i = 0; i < Node.ChildNodes.Count; i++)
  16. {
  17. switch (ChildNode.Name)
  18. {
  19. case "size":
  20.  textBox2.Text += "Size = ";
  21. od.Size = ChildNode.InnerText;
  22. break;
this is my xml file
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--This document contains Ordering Information-->
  3. <OrderList xmlns="http://www.MyCompany.com">
  4.   <Order Order_ID="1" Order_Date="1/2/2013" Buyer_ID="GFS1810">
  5.     <Remark />
  6.     <Item Item_ID="123" Size="M" Color="blue" Quantities="10" ePrice="33.77">Very Nice</Item>
  7.     <Item Item_ID="23423423" Size="M" Color="red" Quantities="10" ePrice="44.10">Please Wear</Item>
  8.     <Item Item_ID="3242" Size="XL" Color="green" Quantities="10" ePrice="67.00">Can Wear</Item>
  9.     <Item Item_ID="3456" Size="S" Color="pink" Quantities="10" ePrice="10.00">SoSo</Item>
  10.   </Order>
  11.   <Order Order_ID="1" Order_Date="1/2/2013" Buyer_ID="GFS1810">
  12.     <Remark />
  13.     <Item Item_ID="4535" Size="L" Color="yellow" Quantities="10" ePrice="22.00">Very Ok</Item>
  14.   </Order>
  15. </OrderList>
Feb 9 '13 #1
2 1994
Mikkeee
94 64KB
The reason you're getting an invalid cast exception is because there are no "Item" child nodes of XmlElement type. The FirstChild of "Item" is the node's inner text. You might want to consider being a little more strict with your data types to eliminate these issues. I would suggest the following to simplify the code.

Expand|Select|Wrap|Line Numbers
  1. XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
  2. nsmgr.AddNamespace("ns", "http://www.MyCompany.com");
  3.  
  4. foreach (XmlNode node in xmlDoc.SelectNodes(@"//ns:OrderList/ns:Order/ns:Item", nsmgr))
  5. {
  6.     Console.WriteLine(node.Attributes["Item_ID"].InnerText);
  7.     Console.WriteLine(node.Attributes["Size"].InnerText);
  8. }
  9.  
Feb 9 '13 #2
TearX
6
thank though my friend had taught me another method but your code work
Feb 11 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: buzz | last post by:
I am attempting to pass data between two asp.net web forms pages. I have found the method to do this on the msdn site here: ...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
6
by: cs_hart | last post by:
I am getting an invalid cast exception - cast from string to type double is not valid. Dim curName As String Dim prevName As String = "" curName = CStr(rows.Item(i).Item(colSchName)) ' extract...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
4
by: Reticulated Ember | last post by:
I have the following code that fails with an invalid cast exception: .... System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage(); msg.BodyFormat = MailFormat.Html; ...
0
by: balukrishnan | last post by:
Hi... I am trying to create an object of Shdocvw.InternetExplorer class, using which I need to launch a particular URL(A JSP Webpage). Then I need to get the document of the page loaded. But...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.