Article ID: 325694 - Last Review: January 22, 2004 - Revision: 2.0

PRB: Parsing XML Containing Invalid Character May Raise ArgumentException

This article was previously published under Q325694

On This Page

Expand all | Collapse all

SYMPTOMS

When you load XML in an XmlDocument object, and the XML contains an invalid character, an exception is raised. The exception may be an XmlException or an ArgumentException, depending on what the invalid character is. For example, if the XML contains 0x13, an XmlException is reported. If the XML contains 0xE9, and the document encoding is US-ASCII, an ArgumentException is reported.

CAUSE

The exceptions are raised for the following two reasons:
  • 0x13 is outside the valid range of XML characters, therefore an XmlException is raised. The following are valid characters:
    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
  • 0xE9 is a valid XML character, however, it is beyond the range that is defined by the US-ASCII encoding. Therefore, an ArgumentException is raised.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET, create a new Microsoft Visual Basic .NET Console Application project.
  2. Paste the following code in the code window:
    Imports System.Xml
    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim xDoc As XmlDocument = New XmlDocument()
    
            'Create XML string containing invalid XML character 0x13
            Dim badData As String = "<?xml version='1.0'?>" & _
                                    "<Data>" & Chr(19) & "</Data>"
    
            'Create byte to insert into stream, set byte equal to 
            'invalid US - ASCII character 0xE9
            Dim byteval As Byte = Byte.Parse("233")
    
    
            Dim stream As MemoryStream = New MemoryStream()
    
            'Create objects that will generate stream. You must use BinaryWriter to insert the invalid character, because StreamWriter would
            'insert a unicode character.  
            Dim swriter As StreamWriter = New StreamWriter(stream, System.Text.Encoding.ASCII)
            Dim bwriter As BinaryWriter = New BinaryWriter(stream, System.Text.Encoding.ASCII)
    
            'Write the first part of the XML instance to the stream.
            swriter.Write("<?xml version='1.0'?>" & _
                                    "<Data>")
            swriter.Flush()
    
            'Write invalid character to stream.
            bwriter.Write(byteval)
            bwriter.Flush()
    
            'Write the rest of the XML to the stream.
            swriter.Write("</Data>")
            swriter.Flush()
            stream.Position = 0
    
            Try
                'Demonstrate an XmlException being raised.
                xDoc.LoadXml(badData)
                Console.Write(xDoc.OuterXml)
            Catch xmlEx As XmlException
                Console.WriteLine("This is an XmlException: " & xmlEx.Message)
            Catch argEx As ArgumentException
                Console.WriteLine("This is an ArgumentException: " & argEx.Message)
            End Try
    
            Try
                'Demonstrate an ArgumentException being raised.
                xDoc.Load(stream)
                Console.Write(xDoc.OuterXml)
            Catch xmlEx As XmlException
                Console.WriteLine("This is an XmlException: " & xmlEx.Message)
            Catch argEx As ArgumentException
                Console.WriteLine("This is an ArgumentException: " & argEx.Message)
            End Try
    
            Console.ReadLine()
        End Sub
    
    End Module
    					
  3. Run the application.
The following is written to the console:
This is an XmlException: '&#x13;', hexadecimal value 0x13, is an invalid character. Line 1, position 28.
				
This is an ArgumentException: Invalid byte was found at byte index 28.

APPLIES TO
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1
Keywords: 
kbprb kbxml KB325694
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations

 

Related Support Centers