BUG: Namespace "xml" is not reserved when using System.Xml (317503)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q317503

SYMPTOMS

If you use an XmlDocument(System.Xml namespace) call to create the xml:lang attribute on an element, the "xml" namespace prefix is not associated with http://www.w3.org/XML/1998/namespace as it needs to. For example, the following code
XmlDocument x = new XmlDocument();
XmlElement c = x.CreateElement("test");
c.SetAttribute("xml:lang", "en-US");
x.AppendChild(c);
Console.Out.WriteLine( "'" + x.DocumentElement.Attributes[0].NamespaceURI + "'" );
Console.Out.WriteLine( "'" + x.DocumentElement.Attributes[0].Prefix + "'"); 
				
produces the following output:
''
'xml'
				
The expected output is as follows:
'http://www.w3.org/XML/1998/namespace'
'xml'
				

RESOLUTION

If you need to persist XML data, use the XmlTextWriter object to output the correct result. See the "More Information" section for sample code.

STATUS

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

MORE INFORMATION

According to the World Wide Web Consortium (W3C) specification (http://www.w3.org/TR/REC-xml-names/#xmlReserved), namespace prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and XML-related specifications.

It is illegal to use "xml" as a prefix to create random elements or attributes other than xml:lang or xml:space. For example, it is illegal to create an attribute xml:this on the element "that" as follows:
<that xml:this="bob" />
				

Steps to Reproduce Behavior

  1. Create a Microsoft C# console project.
  2. Replace the code inside Class1.cs with the following:
    using System;
    using System.Xml;
    
    namespace XMLTest1
    {
    	/// <summary>
    	/// Summary description for Class1.
    	/// </summary>
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			XmlDocument x = new XmlDocument();
    			XmlElement c = x.CreateElement("test");
    			c.SetAttribute("xml:lang", "en-us");
    			x.AppendChild(c);			
    			
    			Console.Out.WriteLine( "'" + x.DocumentElement.Attributes[0].NamespaceURI + "'" );
    			Console.Out.WriteLine( "'" + x.DocumentElement.Attributes[0].Prefix + "'");  
    
    			Console.Out.WriteLine("output from XmlDocument: " + x.InnerXml);
    			
    			Console.Out.WriteLine("\noutput from XmlTextWriter: "); 
    			XmlTextWriter writer = new XmlTextWriter(Console.Out);
    			writer.Formatting = Formatting.Indented;
    			x.WriteTo( writer );
    			writer.Flush();
    			Console.WriteLine();
    
    			return;
    		}
    	}
    }
    					
  3. Compile and then run the project. The output is as follows:
    ''
    'xml'
    output from XmlDocument: <test lang="en-us" />
    
    output from XmlTextWriter:
    <test xml:lang="en-us" />
    						
    The XmlTextWriter writes the correct XML data.

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbbug kbpending KB317503