473,440 Members | 2,021 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,440 software developers and data experts.

Reading XML in C# .NET

Hi all,

I have XML file:
Expand|Select|Wrap|Line Numbers
  1. <events>
  2.   <event>
  3.     <date>sample date 1</date>
  4.     <name>sample name 1</name>
  5.   </event>
  6.   <event>
  7.     <date>sample date 2</date>
  8.     <name>sample name 2</name>
  9.   </event>
  10.   <event>
  11.     <date>sample date 3</date>
  12.     <name>sample name 3</name>
  13.   </event>
  14.   ...
  15. </events>
  16.  
I am using XmlTextReader to read the XML file.
Expand|Select|Wrap|Line Numbers
  1. XmlTextReader reader = new XmlTextReader(FILENAME);
  2.   while (reader.Read()) {
  3.   ...
  4.   }
  5.  
I need to insert the events in the local SQL database, how would i go about looping through the events?

Thanks.
May 3 '07 #1
6 77350
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN....

I'd recommend to use XPathDocument, see this article.
May 3 '07 #2
Welcome to TheScripts TSDN....

I'd recommend to use XPathDocument, see this article.
Thanks,

I've found another thread http://www.thescripts.com/forum/thread176391.html

So my C# code is now:
Expand|Select|Wrap|Line Numbers
  1.             XmlDocument doc = new XmlDocument();
  2.             doc.Load(URLString);
  3.             XmlElement root = doc.DocumentElement;
  4.             XmlNodeList nodes = root.SelectNodes("/events"); 
  5.  
  6.             foreach (XmlNode node in nodes)
  7.             {
  8.                 string date = node["date"].InnerText;
  9.                 string name = node["name"].InnerText;
  10.                 MessageBox.Show("Date:"+date+" Name:"+name);
  11.             }
  12.  
I get the following error:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

Any ideas? thanks.
May 3 '07 #3
dorinbogdan
839 Expert 512MB
I think that XmlNodeList is empty.
Try
Expand|Select|Wrap|Line Numbers
  1. XmlNodeList nodes = root.SelectNodes("//events");
May 3 '07 #4
I think that XmlNodeList is empty.
Try
Expand|Select|Wrap|Line Numbers
  1. XmlNodeList nodes = root.SelectNodes("//events");

That did the trick, thanks for your help :)
May 4 '07 #5
This may works


Expand|Select|Wrap|Line Numbers
  1. XmlNodeList nodes = ele.SelectNodes("/events");
  2.         foreach (XmlNode node in nodes)
  3.         {
  4.             string event1 = node["event"].InnerText;
  5.             foreach (XmlNode n in node)
  6.             {
  7.                 string name = n["date"].InnerText;
  8.                 MessageBox.Show("Event:" + name);
  9.             }
  10.  
  11.             MessageBox.Show("Event:" + event1 );
  12.  
  13.  
  14.         }
Jun 11 '07 #6
@saiprex


Love u!!!!!!!!!!!!!!!!!!!!!!!! Thanks a lot just what i needed!!!!!!!!!!!!!!!!!!!!!!!
Dec 2 '11 #7

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

Similar topics

0
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it...
6
by: Raymond Hettinger | last post by:
Found in a pamphlet at a pre-school: --------------------------------------- Reading improves vocabulary Reading raises cultural literacy through shared knowledge Reading develops writing skills...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
9
by: Mike Reed | last post by:
I must be having a "senile" day! I cannot recall, nor get to work, code to read a cookie's expiration date/time in an ASP page/VBScript. What am I missing? *** Sent via Developersdex...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.