FIX: XPathNavigator.MoveToNextNamespace with XPathNamespaceScope.Local (316808)



The information in this article applies to:

  • Microsoft XML Classes (included with the .NET Framework 1.0)

This article was previously published under Q316808

SYMPTOMS

If an XML node has two namespaces defined as follows:
<ns1:myElement xmlns:ns1='http://namespace1' xmlns:ns2='http://namespace2'><ns1:someElement/></ns1:myElement>
				
When you call the XPathNavigator.MoveToNextNamespace method with the input parameter of XPathNamespaceScope as XPathNamespaceScope.Local to navigate to the next namepace, an incorrect result is returned.

After you call the XPathNavigator.MoveToFirstNamespace(XPathNamespaceScope.Local) method, calling MoveToNextNamespace(XPathNamespaceScope.Local) returns false instead of moving to the next namespace.

RESOLUTION

This problem occurs only with the XmlDocument class; to work around the problem, use the XPathDocument class.

STATUS

This bug was corrected in Microsoft .NET Class Libraries 1.1.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new empty Microsoft Visual C# project, and be sure to add a reference to the System.XML namespace to the empty project.
  2. Paste the following code in a new file and and save it as Q316808.cs. (The file name can be any valid name).
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using System.Diagnostics;
    
    namespace XmlTest
    {
    	class Class1
    	{
    		[STAThread]
    		static void Main(string[] args)
    		{
    			// Create document.
    			string xml = "<ns1:myElement xmlns:ns1='http://namespace1' xmlns:ns2='http://namespace2'><ns1:someElement/></ns1:myElement>";
    			XmlDocument doc = new XmlDocument();
    			doc.LoadXml(xml);
    
    			// Create XPath navigator.
    			XPathNavigator nav = doc.CreateNavigator();
    			
    			Console.WriteLine("Checking navigator\r\n");
    
    			// Reproduce the problem.
    			nav.MoveToRoot();
    			Console.WriteLine("MoveToFirstChild = " + nav.MoveToFirstChild().ToString());
    			Console.WriteLine("Name = " + nav.Name);
    			Console.WriteLine("MoveToFirstNamespace = "+ nav.MoveToFirstNamespace(XPathNamespaceScope.Local).ToString());
    			Console.WriteLine("NodeType = "+ nav.NodeType.ToString());
    			Console.WriteLine("Name = "+ nav.Name);
    			Console.WriteLine("MoveToNextNamespace = "+ nav.MoveToNextNamespace(XPathNamespaceScope.Local).ToString());
    			Console.WriteLine("NodeType = "+ nav.NodeType.ToString());
    			Console.WriteLine("Name = "+ nav.Name);
    
    			Console.WriteLine("\r\n\r\nExpected Results \r\n\r\n");
    			nav.MoveToRoot();
    			Console.WriteLine("MoveToFirstChild = " + nav.MoveToFirstChild().ToString());
    			Console.WriteLine("Name = " + nav.Name);
    			Console.WriteLine("MoveToFirstNamespace = "+ nav.MoveToFirstNamespace().ToString());
    			Console.WriteLine("NodeType = "+ nav.NodeType.ToString());
    			Console.WriteLine("Name = "+ nav.Name);
    			Console.WriteLine("MoveToNextNamespace = "+ nav.MoveToNextNamespace().ToString());
    			Console.WriteLine("NodeType = "+ nav.NodeType.ToString());
    			Console.WriteLine("Name = "+ nav.Name);		
    
    		}
    	}
    }
    					
  3. Compile the project, and then set a breakpoint at the end of the Main function. On the Debug menu, click Start (or just press F5), and you should see the following output from the console window:
    Checking navigator

    MoveToFirstChild = True
    Name = ns1:myElement
    MoveToFirstNamespace = True
    NodeType = Namespace
    Name = ns2
    MoveToNextNamespace = False
    NodeType = Element
    Name = ns1:myElement

    Expected Results :

    MoveToFirstChild = True
    Name = ns1:myElement
    MoveToFirstNamespace = True
    NodeType = Namespace
    Name = ns2
    MoveToNextNamespace = True
    NodeType = Namespace
    Name = ns1
The result returned by MoveToNextNamespace(XPathNamespaceScope.Local) is "False", which is an incorrect result.

NOTE: The namespaces are stored in the reverse order in XMLDocument and XPathDocument. For example, if the XML data has namespaces in the order n1, n2, n3, the MoveToFirstNamespace method moves to n3, MoveToNextNamespace moves to n2, and so on.

Modification Type:MajorLast Reviewed:9/18/2003
Keywords:kbbug KB316808