473,327 Members | 1,919 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,327 software developers and data experts.

Reading complex XML files in C#

Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I am reading have several nested fields:

<?xml version="1.0" encoding="UTF-8" ?>
<xml>
<records>
<record>
<database name="test.enl">test</database>
<contributors>
<authors>
<author>
<style font="default">Stéphane Chatty</style>
</author>
<author>
<style font="default">Patrick Lecoanet</style>
</author>
</authors>
</contributors>
<titles>
<title> ...etc

The actual information that I need out of it is just the values of the author, title.. etc fields. As you can see, they are very nested and the tutorials only help me find the values of fields one level deep.

Here are two of the tutorials I have been following.

http://www.kirupa.com/net/reading_xml_directly_pg1.htm

http://www.c-sharpcorner.com/UploadF...tMellli21.aspx

I would be very appreciative of any advice you could give me :)
Jul 13 '09 #1
4 9005
I have not migrated to .NET 3.0, but in 2.0 you can access data w/ XPath expressions by using an XPathNavigator and I doubt this functionality has been removed

From the C# 2.0 docs:
XPathDocument document = new XPathDocument("path_to/mydocName.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator authorNodes = navigator.Select("/author/text()"); // here use an XPath expression to access your sample xml. This one should return just the text value of the author node or element.

// Select() returns a nodeset. You can loop through it to get values
for (int i = 0; i<authorNodes.length; i++){
Console.WriteLine(authorNodes[i]);// get and use values
}

For XPath tutorials, see w3schools: http://w3schools.com/xpath/default.asp. You can test your XPath expressions at EMC's XQuery Demo [In this demo, XPath expressions are evaluated against sample xml data in an xDB XML Database] at http://137.69.120.115:8080/xquery/ (Click on XQueries tab)
Jul 14 '09 #2
Thanks very much, I'll check out those tutorials I havent heard of XPathNavigator before.

I have no idea what i'm doing though so I will probably be back :)
Jul 14 '09 #3
I think xpath is doing more than i need it to, it seems almost SQL-like and I'm worried about computational efficiency.

I literally just need to read each record, which contains author, title, etc, into a data structure. There must be an easy way to do this? I can do it when "record"s are the root nodes but as you can see from my xml file the actual data is stored several levels deep.

I would like to do it with XmlReader but I'm thinking it might be better to just make my own parser...
Jul 14 '09 #4
PRR
750 Expert 512MB
If your XML file is well formed and you can have a corresponding class for its elements and attributes you can look into xml serialization. Also look at XmlReader and XmlWriter classes.
Jul 15 '09 #5

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

Similar topics

14
by: Peter Galfi | last post by:
Hi! I am looking for a library in Python that would read PDF files and I could extract information from the PDF with it. I have searched with google, but only found libraries that can be used to...
1
by: John Puopolo | last post by:
All, Is there a class in the .NET framework specifically designed for reading configuration files, e.g., MyApp.exe.config? I know that I can read them via XmlReader and the like, but I was...
1
by: Manjunath sp via DotNetMonster.com | last post by:
Hi, How to effectively write and read structures from binary files in .Net? Currently I am using functions like ReadInt32 and the likes to read data from binary file into each elements of a...
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
1
by: Shmuel Shulman | last post by:
Hi I have 2 funny probs that are probably related 1. I can't read the entire field from a dbf file it cuts it at some point (see below) 2. When I use Access or SQL server to read these...
2
by: eddieb7 | last post by:
Hi, I am new to visual Studio 2005 C++ and am looking for some directions on where best to start. I come from a mainly Delphi background and looking to switch to VS 2005 C++ or C#. I am...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
2
by: doublemaster007 | last post by:
Hi How to read binary files in MAC OS? FILE *readFile = fopen("filename", "rb"); then reading it like this: count=fread(readBuffer, sizeof(char), bufferSize, readFile)
0
by: philipdv | last post by:
I have a process reading xml files over an https connection My code to read this xml file is CErrorAndLog.LogAlways("CKlim", "parseXML", "Parsing XML file: " + strFileName) ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.