PRB: Parsing XML Containing Invalid Character May Raise ArgumentException

Exención de responsabilidades de contenido KB retirado

Este artículo se refiere a productos para los que Microsoft ya no ofrece soporte técnico. Por tanto, el presente artículo se ofrece "tal cual" y no será actualizado.

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.
Propiedades

Id. de artículo: 325694 - Última revisión: 21 ene. 2004 - Revisión: 1

Comentarios