473,405 Members | 2,334 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,405 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
2 2686
Try something like this using the XmlDocument to allow for DOM style parsing.
Expand|Select|Wrap|Line Numbers
  1. XmlTextReader reader = new XmlTextReader("XXX.xml");
  2.             XmlDocument xDoc = new XmlDocument();
  3.             xDoc.Load(reader);
  4.             foreach (XmlNode eventNode in xDoc.ChildNodes)
  5.             {
  6.                 // Read the EventNode's children elements
  7.             }
Alternatively you could try using XMLSerialization to serialize the XML into a C# object.
May 6 '07 #2
Try something like this using the XmlDocument to allow for DOM style parsing.
Expand|Select|Wrap|Line Numbers
  1. XmlTextReader reader = new XmlTextReader("XXX.xml");
  2.             XmlDocument xDoc = new XmlDocument();
  3.             xDoc.Load(reader);
  4.             foreach (XmlNode eventNode in xDoc.ChildNodes)
  5.             {
  6.                 // Read the EventNode's children elements
  7.             }
Alternatively you could try using XMLSerialization to serialize the XML into a C# object.
Thanks, but i've ended up using the following code which did the trick :)

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.  
May 7 '07 #3

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.