Sorting XML tags

Discussion in 'XML' started by schaf, Aug 17, 2007.

  1. schaf

    schaf Guest

    Hi NG!
    I'm reading a XML file which contains a namespace. So I create a
    NameSpaceManager to use the .Select() functions on the XmlDocument and
    XmlNode classes.

    Reading like following example:

    string sXPath = string.Format("ab:book[ab:price<='{0}']",
    dPrice.ToString(System.Globalization.CultureInfo.InvariantCulture));
    XmlNodeList xmlbookList = xmlDocBooks.Select(sXPath,
    m_xmlNameSpaceMgr);

    Now I would like to sort my values in the xmlbookList.
    Do I really have to use a while statement for getting my first two
    books with the lowest price?

    I tried the whole thing by using XPathNavigator but it is not possible
    to add the XmlNamespaceManager and so the statements does not work as
    soon as I have a Namespace in the XML file.

    Do you have a hint ?
    Regards
     
    schaf, Aug 17, 2007
    #1
    1. Advertisements

  2. schaf wrote:

    > string sXPath = string.Format("ab:book[ab:price<='{0}']",
    > dPrice.ToString(System.Globalization.CultureInfo.InvariantCulture));
    > XmlNodeList xmlbookList = xmlDocBooks.Select(sXPath,
    > m_xmlNameSpaceMgr);


    Note that with XPath 1.0 <= works on numbers but not on strings so
    usually you want string.Format("ab:book[ab:price<={0}]", ...). A string
    would be converted to number anyway if used as an operand to <, >, <=, >=.

    > Now I would like to sort my values in the xmlbookList.
    > Do I really have to use a while statement for getting my first two
    > books with the lowest price?
    >
    > I tried the whole thing by using XPathNavigator but it is not possible
    > to add the XmlNamespaceManager and so the statements does not work as
    > soon as I have a Namespace in the XML file.


    You can use an XmlNamespaceManager with an IXmlNamespaceResolver with
    XPathNavigator:
    <http://msdn2.microsoft.com/en-us/library/6k4x060d.aspx>

    And you can pass in an XPathExpression on which you can define a sort
    and a namespace manager
    <http://msdn2.microsoft.com/en-us/library/system.xml.xpath.xpathexpression.addsort.aspx>
    <http://msdn2.microsoft.com/en-us/library/system.xml.xpath.xpathexpression.setcontext.aspx>
    So that way you set up the expression with all details first and then
    pass it to the Select method of XPathNavigator.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 17, 2007
    #2
    1. Advertisements

  3. schaf

    schaf Guest

    Hi Martin!

    > You can use an XmlNamespaceManager with an IXmlNamespaceResolver with
    > XPathNavigator:
    > <http://msdn2.microsoft.com/en-us/library/6k4x060d.aspx>
    >
    > And you can pass in an XPathExpression on which you can define a sort
    > and a namespace manager
    > <http://msdn2.microsoft.com/en-us/library/system.xml.xpath.xpathexpres...>
    > <http://msdn2.microsoft.com/en-us/library/system.xml.xpath.xpathexpres...>
    > So that way you set up the expression with all details first and then
    > pass it to the Select method of XPathNavigator.


    Unfortunately I'm using .NET 1.1.

    I have seen, that I can set the namespaceManager with SetContext:

    XPathNavigator nav = xmlBooks.CreateNavigator();
    XPathExpression xPathEx = nav.Compile("ab:book[ab:price>='1.73']");
    xPathEx.AddSort("ab:price", XmlSortOrder.Ascending, XmlCaseOrder.None,
    "", XmlDataType.Number);
    xPathEx.SetContext(m_xmlNameSpaceMgr);
    XPathNodeIterator iter = nav.Select(xPathEx);
    Console.WriteLine(iter);

    But iter does not contain some nodes. If I do not add the AddSort Line
    it works fine.
    What is wrong with my sort?
     
    schaf, Aug 17, 2007
    #3
  4. schaf wrote:

    > XPathNavigator nav = xmlBooks.CreateNavigator();
    > XPathExpression xPathEx = nav.Compile("ab:book[ab:price>='1.73']");
    > xPathEx.AddSort("ab:price", XmlSortOrder.Ascending, XmlCaseOrder.None,
    > "", XmlDataType.Number);
    > xPathEx.SetContext(m_xmlNameSpaceMgr);
    > XPathNodeIterator iter = nav.Select(xPathEx);
    > Console.WriteLine(iter);
    >
    > But iter does not contain some nodes. If I do not add the AddSort Line
    > it works fine.
    > What is wrong with my sort?


    The only thing that looks wrong to me is the Console.WriteLine(iter),
    you should use MoveNext on iter to check whether any nodes are contained
    in the node iterator. Otherwise I am not sure what is wrong but it is
    hard to tell exactly where things could go wrong, I would need to see
    the XML, what xmlBooks is to judge whether the relative XPath expression
    ab:book makes sense for instance.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Aug 17, 2007
    #4
  5. schaf

    schaf Guest

    Hi all!
    I have the solution...sorting works fine!

    XPathNavigator nav = xmlBooks.CreateNavigator();
    XPathExpression xPathEx = nav.Compile("ab:book[ab:price>='1.73']");
    xPathEx.SetContext(m_xmlNameSpaceMgr);
    XPathExpression xSort = nav.Compile("ab:price");
    xSort.SetContext(m_xmlNameSpaceMgr);
    xPathEx.AddSort(xSort, XmlSortOrder.Ascending, XmlCaseOrder.None, "",
    XmlDataType.Number);
    XPathNodeIterator iter = nav.Select(xPathEx);
    while (iter.MoveNext()) {
    XPathNavigator currNav = iter.Current;
    Console.WriteLine(currNav);
    }

    Thanks
     
    schaf, Aug 17, 2007
    #5
  6. >>> On 8/17/2007 at 10:57 AM, in message
    <>,
    schaf<> wrote:
    > Hi all!
    > I have the solution...sorting works fine!
    >
    > XPathNavigator nav = xmlBooks.CreateNavigator();
    > XPathExpression xPathEx = nav.Compile("ab:book[ab:price>='1.73']");
    > xPathEx.SetContext(m_xmlNameSpaceMgr);
    > XPathExpression xSort = nav.Compile("ab:price");
    > xSort.SetContext(m_xmlNameSpaceMgr);
    > xPathEx.AddSort(xSort, XmlSortOrder.Ascending, XmlCaseOrder.None, "",
    > XmlDataType.Number);
    > XPathNodeIterator iter = nav.Select(xPathEx);
    > while (iter.MoveNext()) {
    > XPathNavigator currNav = iter.Current;
    > Console.WriteLine(currNav);
    > }



    Not relevant to the issue at hand, but might you also want to change the
    following:

    XPathNodeIterator iter = nav.Select(xPathEx);
    while (iter.MoveNext()) {
    XPathNavigator currNav = iter.Current;
    Console.WriteLine(currNav);
    }

    ....to something like this?

    foreach (XPathNavigator currNav in nav.Select(xPathEx)) {
    Console.WriteLine(currNav);
    }

    Not tested. See the following for more info:
    http://www.topxml.com/rbnews/XmlDocument/re-7004_foreach-and-XPathNodeIterat
    or---finally-together.aspx

    I'm a fan of "foreach", so the original code jumped out at me.

    Frank
     
    Frank Swarbrick, Aug 21, 2007
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. terry
    Replies:
    0
    Views:
    465
    terry
    Jul 9, 2003
  2. yannicklaclau
    Replies:
    2
    Views:
    594
    yannicklaclau
    Feb 28, 2005
  3. Frank Stallone
    Replies:
    4
    Views:
    766
    Andy Dingley
    Mar 23, 2005
  4. Thierry
    Replies:
    5
    Views:
    561
    Peter Flynn
    Feb 15, 2006
  5. Shyran
    Replies:
    1
    Views:
    443
    Shyran
    Nov 15, 2006
  6. theintrepidfox
    Replies:
    0
    Views:
    356
    theintrepidfox
    Nov 26, 2007
  7. Joah Senegal

    Printing XML string With XML tags

    Joah Senegal, Apr 23, 2008, in forum: C++
    Replies:
    1
    Views:
    843
    Christopher
    Apr 23, 2008
  8. Erik Wasser
    Replies:
    5
    Views:
    817
    Peter J. Holzer
    Mar 5, 2006
Loading...