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.
The exceptions are raised for the following two reasons:
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 = "<?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
- Run the application.
The following is written to the console:
This is an XmlException: '', hexadecimal value 0x13, is an invalid character. Line 1, position 28.
This is an ArgumentException: Invalid byte was found at byte index 28.