Microsoft KB Archive/316656

From BetaArchive Wiki

Article ID: 316656

Article Last Modified on 2/19/2007



APPLIES TO

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0



This article was previously published under Q316656

This article refers to the following Microsoft .NET Framework Class Library namespaces:

  • System.Xml
  • System.Xml.XPath


SYMPTOMS

When you use a string to specify the sort key expression parameter in a call to the AddSort method of the System.Xml.XPath.XPathExpression object, the XML data is not sorted.

CAUSE

This problem is only noticed when the specified sort key expression contains XML element and attribute names that are prefixed with namespaces. When a string is used to specify the sort key expression, the namespace prefixes are not correctly resolved. This behavior is noticed even when the System.Xml.XPath.XPathExpression object is associated with a System.Xml.XmlNamespaceManager object to which the namespace prefix definitions have been added.

RESOLUTION

Use a second System.Xml.XPath.XPathExpression object to specify the sort key expression parameter when you execute the AddSort method.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Use the following code to create and save an XML document named "Testsort.xml" in the root folder of your hard disk:

    <?xml version="1.0" encoding="utf-8" ?> 
    <type xmlns:test='urn:Test'>
       <elements>
          <element name='element_1' test:order='1'>1</element>
          <element name='element_6' test:order='6'>6</element>
          <element name='element_7' test:order='5'>5</element>
          <element name='element_8' test:order='4'>4</element>
          <element name='element_9' test:order='3'>3</element>
       </elements>
    </type>
                        
  2. Use Microsoft Visual Studio .NET to create a new Microsoft Visual Basic .NET Windows Application project.
  3. Import the System.Xml and System.Xml.XPath namespaces by adding the following statements at the top of the Form1.vb class module:

    Imports System.Xml
    Imports System.Xml.XPath
                        
  4. Drag a Button control from the toolbox to the designer surface of Form1.vb.
  5. Paste the following code in the Click event procedure of the command button to sort the data in Testsort.xml on the order attribute of the <element> element in descending order. Note that the order attribute is prefixed with a custom namespace prefix (test) and that a string is used in the call to the AddSort method to specify the sort expression.

    Dim doc As New XPathDocument("C:\Testsort.xml")
    Dim nav As XPathNavigator = doc.CreateNavigator()
    
    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
    nsmgr.AddNamespace("test", "urn:Test")
    
    Dim expr As XPathExpression
    expr = nav.Compile("/type/elements/element")
    expr.SetContext(nsmgr)
    
    'Dim expr2 As XPathExpression = nav.Compile("@test:order")
    'expr2.SetContext(nsmgr)
    
    expr.AddSort("@test:order", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
    expr.SetContext(nsmgr)
    
    Dim iterator As XPathNodeIterator = nav.Select(expr)
    Do While iterator.MoveNext()
          System.Diagnostics.Debug.WriteLine(iterator.Current.Value)
    Loop
                        
  6. Save and then run the project. Click the button when the form is displayed. On observing the result generated to the Output window, notice that the data was not sorted.
  7. Uncomment the two lines of code that create an instance of the second XPathExpression object (expr2) and execute its SetContext method to associate it with the XmlNamespaceManager.
  8. Replace the call to the AddSort method of the first XPathExpression object (expr) with the following code:

    expr.AddSort(expr2, XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
                            

    You can also remove the expr.SetContext statement that follows the call to the AddSort method.

  9. Save and then run the project. Click the button when the form is displayed. Notice that the data is sorted in descending order as required.


Keywords: kbbug kbpending KB316656