Microsoft KB Archive/324996

= BUG: XmlNamespaceManager Does Not Correctly Atomize Strings During Namespace Lookups =

Article ID: 324996

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1

-



This article was previously published under Q324996



SYMPTOMS
The LookupNamespace and LookupPrefix methods of the XmlNamespaceManager class may return a null value even when a matching namespace definition has been added to the XmlNamespaceManager object. This problem is only noticed when you use the NameTable of an XmlDocument to construct the XmlNamespaceManager object instance.



CAUSE
XmlNamespaceManager does not correctly atomize strings while it executes namespace lookups when the NameTable of an XmlDocument is used to construct the XmlNamespaceManager object and a string value is used to directly specify the namespace prefix or URI to look up.



RESOLUTION
Use the Get method of the NameTable property of the XmlNamespaceManager object to supply the prefix and URI parameters when you execute the LookupNamespace and LookupPrefix methods. Alternatively, you can supply a new NameTable object instance when you construct the XmlNamespaceManager object.



STATUS
Microsoft has confirmed that this is a bug in the implementation of the XmlNamespaceManager class in the System.Xml Microsoft .NET Framework namespace.



Steps to Reproduce the Behavior
To re-create the described problem by using a Visual Basic .NET Console Application project, follow these steps:  In Microsoft Visual Studio .NET, create a new Microsoft Visual Basic .NET Console Application project named XmlNsMgrRepro.  Import the following namespace: Imports System.Xml   Use the following XML to create and add an XML file named Booksns.xml to the project: 

  The Autobiography of Benjamin Franklin  <auth:first-name>Benjamin</auth:first-name> <auth:last-name>Franklin</auth:last-name> </auth:author> 8.99  <book genre=&quot;novel&quot; publicationdate=&quot;1967&quot; ISBN=&quot;0-201-63361-2&quot;> The Confidence Man <auth:author> <auth:first-name>Herman</auth:first-name> <auth:last-name>Melville</auth:last-name> <auth:degree from=&quot;Trenton U&quot;>B.A.</auth:degree> </auth:author> 11.99  <book genre=&quot;philosophy&quot; publicationdate=&quot;1991&quot; ISBN=&quot;1-861001-57-6&quot;> The Gorgias <auth:author> <auth:first-name>William</auth:first-name> <auth:last-name>Plato</auth:last-name> <auth:degree from=&quot;Harvard&quot;>Ph.D.</auth:degree> </auth:author> 9.99                    </li>  Paste the following code in the Sub Main procedure: Dim doc As New XmlDocument<BR/> Dim FilePath As String = CurDir + &quot;\..&quot; doc.Load(FilePath + &quot;\booksNS.xml&quot;)

Dim AuthorNodes As XmlNodeList Dim AuthorNode As XmlNode

Dim XmlNsMgr As New XmlNamespaceManager(doc.NameTable)

'SUPPLYING A NEW NameTable OBJECT INSTANCE WILL ALSO FIX THE PROBLEM. 'Dim XmlNsMgr As New XmlNamespaceManager(New NameTable)

XmlNsMgr.AddNamespace(&quot;bk&quot;, &quot;MyUrn:books&quot;) XmlNsMgr.AddNamespace(&quot;auth&quot;, &quot;MyUrn:Authors&quot;)

Console.WriteLine(&quot;Prefix for MyUrn:books : &quot; & XmlNsMgr.LookupPrefix(&quot;MyUrn:books&quot;)) 'RETURNS NULL Console.WriteLine(&quot;Namespace URI for bk prefix : &quot; & XmlNsMgr.LookupNamespace(&quot;bk&quot;)) 'RETURNS THE NAMESPACE URI

Console.WriteLine(&quot;---&quot;)

Console.WriteLine(&quot;Prefix for MyUrn:Authors : &quot; & XmlNsMgr.LookupPrefix(&quot;MyUrn:Authors&quot;)) 'RETURNS NULL Console.WriteLine(&quot;Namespace URI for auth prefix : &quot; & XmlNsMgr.LookupNamespace(&quot;auth&quot;)) 'RETURNS NULL

'DOING THE FOLLOWING INSTEAD OF SUPPLYING A NEW NameTable OBJECT 'WHEN CONSTRUCTING THE XmlNamespaceManager OBJECT WILL ALWAYS WORK AS EXPECTED.

'Console.WriteLine(&quot;Prefix for MyUrn:books : &quot; & XmlNsMgr.LookupPrefix(XmlNsMgr.NameTable.Get(&quot;MyUrn:books&quot;))) 'Console.WriteLine(&quot;Namespace URI for bk prefix : &quot; & XmlNsMgr.LookupNamespace(XmlNsMgr.NameTable.Get(&quot;bk&quot;)))

'Console.WriteLine(&quot;---&quot;)

'Console.WriteLine(&quot;Prefix for MyUrn:Authors : &quot; & XmlNsMgr.LookupPrefix(XmlNsMgr.NameTable.Get(&quot;MyUrn:Authors&quot;))) 'Console.WriteLine(&quot;Namespace URI for auth prefix : &quot; & XmlNsMgr.LookupNamespace(XmlNsMgr.NameTable.Get(&quot;auth&quot;)))

Dim response As String = Console.ReadLine </li> Review the code and read the inline comments. Notice that the active code uses string values to directly supply the namespace and prefix parameters when it executes the LookupPrefix and LookupNamespace methods of the XmlNamespaceManager object. Also notice that the NameTable of the XmlDocument in which the source XML is loaded is used to create an instance of the XmlNamespaceManager object.</li> Save and build the project.</li> Execute the project and view the output. Notice that the LookupPrefix method does not return the prefix names (it returns NULLs) for both of the specified namespace URIs (MyUrn:books and MyUrn:Authors). Also notice that the LookupNamespace method returns the namespace URI only for the first prefix definition that is added to the XmlNamespaceManager (the prefix bk). The namespace URI for the second prefix definition (the prefix auth) is not returned as expected.</li> Press any key to close the console window and stop the project.</li> Comment the currently active Console.WriteLine statements in the code and uncomment the Console.WriteLine statements that use the Get method of the NameTable object property of the XmlNamespaceManager object to supply the prefix and URI parameters when the LookupNamespace and LookupPrefix methods are executed.</li> Save and rebuild the project.</li> Execute the project and examine the output. This time, both the LookupNamespace and LookupPrefix method calls return the expected output. Press any key to close the console window and stop the project. Note from the comments in the code that another workaround is to supply a new NameTable object instance when the XmlNamespaceManager object is instantiated. When you do this, string values can be used directly to supply the namespace URI and prefix parameters while the LookupPrefix and LookupNamespace methods are executed.</li></ol>

Keywords: kbbug kbpending KB324996

-

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

© Microsoft Corporation. All rights reserved.