Microsoft KB Archive/316656

= BUG: Data Not Sorted When You Use Namespace Prefix in String Passed to XPathExpression.AddSort =

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.



Steps to Reproduce the Behavior
  Use the following code to create and save an XML document named &quot;Testsort.xml&quot; in the root folder of your hard disk:   1 6 5 4 3 </li> <li>Use Microsoft Visual Studio .NET to create a new Microsoft Visual Basic .NET Windows Application project.</li> <li> 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 </li> <li>Drag a Button control from the toolbox to the designer surface of Form1.vb.</li> <li> 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 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(&quot;C:\Testsort.xml&quot;) Dim nav As XPathNavigator = doc.CreateNavigator

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable) nsmgr.AddNamespace(&quot;test&quot;, &quot;urn:Test&quot;)

Dim expr As XPathExpression expr = nav.Compile(&quot;/type/elements/element&quot;) expr.SetContext(nsmgr)

'Dim expr2 As XPathExpression = nav.Compile(&quot;@test:order&quot;) 'expr2.SetContext(nsmgr)

expr.AddSort(&quot;@test:order&quot;, XmlSortOrder.Descending, XmlCaseOrder.None, &quot;&quot;, XmlDataType.Number) expr.SetContext(nsmgr)

Dim iterator As XPathNodeIterator = nav.Select(expr) Do While iterator.MoveNext System.Diagnostics.Debug.WriteLine(iterator.Current.Value) Loop </li> <li>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.</li> <li>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.</li> <li> Replace the call to the AddSort method of the first XPathExpression object (expr) with the following code: expr.AddSort(expr2, XmlSortOrder.Descending, XmlCaseOrder.None, &quot;&quot;, XmlDataType.Number) You can also remove the expr.SetContext statement that follows the call to the AddSort method. </li> <li>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.</li></ol>

Keywords: kbbug kbpending KB316656

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.