By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,366 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,366 IT Pros & Developers. It's quick & easy.

Reading complex XML files in C#

P: 15
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
Share this Question
Share on Google+
4 Replies


P: 1
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

P: 15
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

P: 15
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

Expert 100+
P: 750
PRR
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

Post your reply

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