Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. An XML newbie's problem

An XML newbie's problem

Scheduled Pinned Locked Moved C#
xmlhelpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    Hi, I am not sure this is the right place to post my problem but I think there are some people out there who can solve my newbie problem. I am posting my source xml and the code file. ////////////////XML File/////////////// Pride And Prejudice Jane Austen 24.95 The Handmaid's Tale Margaret Atwood 29.95 Emma Jane Austen 19.95 Sense and Sensibility Jane Austen 19.95 ////////////////Code File///////////////// using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("booksort2.xml"); XPathNavigator nav = doc.CreateNavigator(); //Select all books by Jane Austen. XPathExpression expr; expr = nav.Compile("descendant::book[@publicationdate='1992']"); //Sort the selected books by title. expr.AddSort("@ISBN", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text); //Display the selection. XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); nav2.MoveToFirstChild(); Console.WriteLine("Book ISBN: {0}", nav2.Value); } } } /* I want an output like this: ////////////////////////////////// Book ISBN: 1-861001-57-8 Book ISBN: 1-861002-30-1 ////////////////////////////////// But I get this??? ////////////////////////////////// Book ISBN: Pride And Prejudice Book ISBN: The Handmaid's Tale ////////////////////////////////// */ Any help will be appreciated, Thanx

    K 1 Reply Last reply
    0
    • C cemlouis

      Hi, I am not sure this is the right place to post my problem but I think there are some people out there who can solve my newbie problem. I am posting my source xml and the code file. ////////////////XML File/////////////// Pride And Prejudice Jane Austen 24.95 The Handmaid's Tale Margaret Atwood 29.95 Emma Jane Austen 19.95 Sense and Sensibility Jane Austen 19.95 ////////////////Code File///////////////// using System; using System.IO; using System.Xml; using System.Xml.XPath; public class Sample { public static void Main() { XPathDocument doc = new XPathDocument("booksort2.xml"); XPathNavigator nav = doc.CreateNavigator(); //Select all books by Jane Austen. XPathExpression expr; expr = nav.Compile("descendant::book[@publicationdate='1992']"); //Sort the selected books by title. expr.AddSort("@ISBN", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text); //Display the selection. XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); nav2.MoveToFirstChild(); Console.WriteLine("Book ISBN: {0}", nav2.Value); } } } /* I want an output like this: ////////////////////////////////// Book ISBN: 1-861001-57-8 Book ISBN: 1-861002-30-1 ////////////////////////////////// But I get this??? ////////////////////////////////// Book ISBN: Pride And Prejudice Book ISBN: The Handmaid's Tale ////////////////////////////////// */ Any help will be appreciated, Thanx

      K Offline
      K Offline
      Kentamanos
      wrote on last edited by
      #2

      I'm not an expert on the XmlNavigator class (I usually just use SelectNodes and get a collection of XmlNodes), but I think I see your problem. You're finding a bunch of book nodes, and then you're moving to the first child, expecting to find the ISBN. The first child (child node) of book is the title node. What you want is an attribute, and not a child (isbn is an attribute and not a child node). Take a look at XmlNavigator.GetAttribute. I think that will allow you to get the ISBN.


      I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
      -David St. Hubbins

      C 1 Reply Last reply
      0
      • K Kentamanos

        I'm not an expert on the XmlNavigator class (I usually just use SelectNodes and get a collection of XmlNodes), but I think I see your problem. You're finding a bunch of book nodes, and then you're moving to the first child, expecting to find the ISBN. The first child (child node) of book is the title node. What you want is an attribute, and not a child (isbn is an attribute and not a child node). Take a look at XmlNavigator.GetAttribute. I think that will allow you to get the ISBN.


        I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
        -David St. Hubbins

        C Offline
        C Offline
        cemlouis
        wrote on last edited by
        #3

        Thank you it is because of my language I think I didn't know what the meaning of attribute... I changed the loop with the one below and it worked :) Thanx anyway... while (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current.Clone(); nav2.MoveToAttribute("ISBN", "urn:samples" ); Console.WriteLine("Book ISBN: {0}", nav2.Value); }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups