An XML newbie's problem
-
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
-
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
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 -
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. HubbinsThank 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); }