Reading complex XML files in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ducttape
    New Member
    • May 2009
    • 15

    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.



    In this tutorial, you will learn how to read and write XML documents in Microsoft .NET using C# language.


    I would be very appreciative of any advice you could give me :)
  • fredemc
    New Member
    • Jul 2009
    • 1

    #2
    You can use XPath in C#

    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.Create Navigator();
    XPathNodeIterat or authorNodes = navigator.Selec t("/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.l ength; i++){
    Console.WriteLi ne(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)

    Comment

    • ducttape
      New Member
      • May 2009
      • 15

      #3
      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 :)

      Comment

      • ducttape
        New Member
        • May 2009
        • 15

        #4
        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...

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          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.

          Comment

          Working...