PRB: You Receive XmlTextReader Error Message When Colon Appears in Processing Instruction (318351)



The information in this article applies to:

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

This article was previously published under Q318351
The following .NET Framework Class LIbrary namespace is referenced in this aritcle:

System.XML

SYMPTOMS

If you use the XmlTextReader to read an Extensible Markup Language (XML) document, and the document contains a processing instruction with a colon, you may receive the following error message:
'xml:stylesheet' is an invalid name for a processing instruction. Line 1, position 3.

CAUSE

This behavior is by design.

RESOLUTION

To work around this problem, do one of the following:
  • Do not use colon in the processing instruction. Instead, use a hyphen. Both Microsoft and W3C recommend this method:
    <?xml:stylesheet ...?> should be changed to <?xml-stylesheet ...?> 
    						
    For more information about the W3C recommendation, browse to the following W3C Web site:
  • If you cannot change the XML, turn off the namespace on XmlTextReader before you read the XML, and then set the namespace back to its original value after you finish reading:
    //before calling read, set 
    XmlTextReader.Namespace = false 
    
    //after you have read, set 
    XmlTextReader.Namespaces=true
    					

MORE INFORMATION

Steps to Reproduce Behavior

  1. Open Microsoft Visual Studio .NET.
  2. Create a new Windows application in Microsoft Visual Basic .NET. Form1 is added to the project by default.
  3. Place a Command button on Form1. Change the Name property of the button to btnTest.
  4. Use the Imports statement on the System.Xml namespaces, so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the "General Declarations" section of Form1:
    Imports System.Xml
    					
  5. Copy and paste the following code in the btnTest_Click event:
            Try
                Dim reader As New XmlTextReader("c:\textr.xml")
                While reader.Read()
                    If reader.NodeType = XmlNodeType.Element Then
                        MessageBox.Show(reader.Name)
                    End If
                End While
            Catch ex As Exception
                MessageBox.Show(ex.Message())
            End Try
    					
  6. Copy and paste the following XML into Notepad, and then save the file as Textr.xml in your C: root directory:
    <?xml:stylesheet type="text/xsl" href="test.xsl"?>
    <xml>
    <library>
      <book genre='novel' ISBN='1-861001-57-5'>
         <title>Pride And Prejudice</title>
      </book>
      <book genre='novel' ISBN='1-81920-21-2'>
         <title>Hook</title>
      </book>
    </library> 
    </xml>
    					
  7. Save the project, and then run the project.
  8. Click btnTest, and then notice the error message.
  9. Open Textr.xml, and then modify the following line:
    <?xml:stylesheet type="text/xsl" href="test.xsl"?>
    						
    to the following:
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
    						
  10. Save the XML file, and then run the project.
  11. Click btnTest, and then notice the element names.
Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

Modification Type:MajorLast Reviewed:9/17/2003
Keywords:kbprb KB318351