Microsoft KB Archive/325694

= PRB: Parsing XML Containing Invalid Character May Raise ArgumentException =

Article ID: 325694

Article Last Modified on 1/22/2004

-

APPLIES TO


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

-



This article was previously published under Q325694



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.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, create a new Microsoft Visual Basic .NET Console Application project.  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 = &quot;&quot; & _ &quot; &quot;

'Create byte to insert into stream, set byte equal to        'invalid US - ASCII character 0xE9 Dim byteval As Byte = Byte.Parse(&quot;233&quot;)

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(&quot;<?xml version='1.0'?>&quot; & _                               &quot; &quot;) swriter.Flush stream.Position = 0

Try 'Demonstrate an XmlException being raised. xDoc.LoadXml(badData) Console.Write(xDoc.OuterXml) Catch xmlEx As XmlException Console.WriteLine(&quot;This is an XmlException: &quot; & xmlEx.Message) Catch argEx As ArgumentException Console.WriteLine(&quot;This is an ArgumentException: &quot; & argEx.Message) End Try

Try 'Demonstrate an ArgumentException being raised. xDoc.Load(stream) Console.Write(xDoc.OuterXml) Catch xmlEx As XmlException Console.WriteLine(&quot;This is an XmlException: &quot; & xmlEx.Message) Catch argEx As ArgumentException Console.WriteLine(&quot;This is an ArgumentException: &quot; & argEx.Message) End Try

Console.ReadLine End Sub

End Module </li> Run the application.</li></ol>

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.

Keywords: kbxml kbprb KB325694

-

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

© Microsoft Corporation. All rights reserved.