Hexadecimal value 0x is an invalid character

kick it on DotNetKicks.com

MIT LICENSED

Ever get a System.Xml.XmlException that says:

“Hexadecimal value 0x[whatever] is an invalid character”

…when trying to load a XML document using one of the .NET XML API objects like XmlReader, XmlDocument, or XDocument? Was “0x[whatever]” by chance one of these characters?

0x00

0x01

0x02

0x03

0x04

0x05

0x06

0x07

0x08

0x0B

0x0C

0x0E

0x0F

0x10

0x11

0x12

0x13

0x14

0x15

0x1A

0x1B

0x1C

0x1D

0x1E

0x1F

0x16

0x17

0x18

0x19

0x7F

The problem that causes these XmlExceptions is that the data being read or loaded contains characters that are illegal according to the XML specifications. Almost always, these characters are in the ASCII control character range (think whacky characters like null, bell, backspace, etc). These aren’t characters that have any business being in XML data; they’re illegal characters that should be removed, usually having found their way into the data from file format conversions, like when someone tries to create an XML file from Excel data, or export their data to XML from a format that may be stored as binary.

The decimal range for ASCII control characters is 0 – 31, and 127. Or, in hex, 0x00 – 0x1F. (The control character 0x7F is not disallowed, but its use is “discouraged” to avoid compatibility issues.) If any character in the string or stream that contains the XML data contains one of these control characters, an XmlException will be thrown by whatever System.Xml or System.Xml.Linq class (e.g. XmlReader, XmlDocument, XDocument) is trying to load the XML data. In fact, if XML data contains the character ‘\b’ (bell), your motherboard will actually make the bell sound before the XmlException is thrown.

There are a few exceptions though: the formatting characters ‘\n’, ‘\r’, and ‘\t’ are not illegal in XML, per the 1.0 and 1.1 specifications, and therefore do not cause this XmlException. Thus, if you’re encountering XML data that is causing an XmlException because the data “contains invalid characters”, the feeds you’re processing need to be sanitized of illegal XML characters per the XML 1.0 specification (which is what System.Xml conforms to—not XML 1.1) should be removed. The methods below will accomplish this:

/// <summary>
/// Remove illegal XML characters from a string.
/// </summary>
public string SanitizeXmlString(string xml)
{
	if (xml == null)
	{
		throw new ArgumentNullException("xml");
	}
	
	StringBuilder buffer = new StringBuilder(xml.Length);
	
	foreach (char c in xml)
	{
		if (IsLegalXmlChar(c))
		{
			buffer.Append(c);
		}
	}
		
	return buffer.ToString();
}

/// <summary>
/// Whether a given character is allowed by XML 1.0.
/// </summary>
public bool IsLegalXmlChar(int character)
{
	return
	(
		 character == 0x9 /* == '\t' == 9   */          ||
		 character == 0xA /* == '\n' == 10  */          ||
		 character == 0xD /* == '\r' == 13  */          ||
		(character >= 0x20    && character <= 0xD7FF  ) ||
		(character >= 0xE000  && character <= 0xFFFD  ) ||
		(character >= 0x10000 && character <= 0x10FFFF)
	);
}

Useful as these methods are, don’t go off pasting them into your code anywhere. Create a class instead. Here’s why: let’s say you use the routine to sanitize a string in one section of code. Then another section of code uses that same string that has been sanitized. How does the other section positively know that the string doesn’t contain any control characters anymore, without checking? It doesn’t. Who knows where that string has been (if it’s been sanitized) before it gets to a different routine, further down the processing pipeline. Program defensive and agnostically. If the sanitized string isn’t a string and is instead a different type that represents sanitized strings, you can guarantee that the string doesn’t contain illegal characters.

Now, if the strings that need to be sanitized are being retrieved from a Stream, via a TextReader, for example, we can create a custom StreamReader class that will skip over illegal characters. Let’s say that you’re retrieving XML like so:

string xml;

using (WebClient downloader = new WebClient())
{
	using (TextReader reader =
		new StreamReader(downloader.OpenRead(uri)))
	{
		xml = reader.ReadToEnd();
	}
}

// Do something with xml...

You could use the sanitizing methods above like this:

string xml;

using (WebClient downloader = new WebClient())
{
	using (TextReader reader =
		new StreamReader(downloader.OpenRead(uri)))
	{
		xml = reader.ReadToEnd();
	}
}

// Sanitize the XML

xml = SanitizeXmlString(xml);

// Do something with xml...

But creating a class that inherits from StreamReader and avoiding the costly string-building operation performed by SanitizeXmlString() is much more efficient. The class will have to override a couple methods when it’s finished, but when it is, a Stream could be consumed and sanitized like this instead:

string xml;

using (WebClient downloader = new WebClient())
{
	using(XmlSanitizingStream reader =
		new XmlSanitizingStream(downloader.OpenRead(uri)))
	{
		xml = reader.ReadToEnd()
	}
}

// xml contains no illegal characters

The declaration for this XmlSanitizingStream, with IsLegalXmlChar() that we’ll need, looks like:

public class XmlSanitizingStream : StreamReader
{
	// Pass 'true' to automatically detect encoding using BOMs.
	// BOMs: http://en.wikipedia.org/wiki/Byte-order_mark

	public XmlSanitizingStream(Stream streamToSanitize)
		: base(streamToSanitize, true)
	{ }

	/// <summary>
	/// Whether a given character is allowed by XML 1.0.
	/// </summary>
	public static bool IsLegalXmlChar(int character)
	{
		return
		(
			 character == 0x9 /* == '\t' == 9   */          ||
			 character == 0xA /* == '\n' == 10  */          ||
			 character == 0xD /* == '\r' == 13  */          ||
			(character >= 0x20    && character <= 0xD7FF  ) ||
			(character >= 0xE000  && character <= 0xFFFD  ) ||
			(character >= 0x10000 && character <= 0x10FFFF)
		);
	}

	// ...

To get this XmlSanitizingStream working correctly, we’ll first need to override two methods integral to the StreamReader: Peek(), and Read(). The Read method should only return legal XML characters, and Peek() should skip past a character if it’s not legal.

	private const int EOF = -1;

	public override int Read()
	{
		// Read each char, skipping ones XML has prohibited

		int nextCharacter;

		do
		{
			// Read a character

			if ((nextCharacter = base.Read()) == EOF)
			{
				// If the char denotes end of file, stop
				break;
			}
		}

		// Skip char if it's illegal, and try the next

		while (!XmlSanitizingStream.
		        IsLegalXmlChar(nextCharacter));

		return nextCharacter;
	}

	public override int Peek()
	{
		// Return next legal XML char w/o reading it 

		int nextCharacter;

		do
		{
			// See what the next character is 
			nextCharacter = base.Peek();
		}
		while
		(
			// If it's illegal, skip over 
			// and try the next.

			!XmlSanitizingStream
			.IsLegalXmlChar(nextCharacter) &&
			(nextCharacter = base.Read()) != EOF
		);

		return nextCharacter;

	}

Next, we’ll need to override the other Read* methods (Read, ReadToEnd, ReadLine, ReadBlock). These all use Peek() and Read() to derive their returns. If they are not overridden, calling them on XmlSanitizingStream will invoke them on the underlying base StreamReader. That StreamReader will then use its Peek() and Read() methods, not the XmlSanitizingStream’s, resulting in unsanitized characters making their way through.

To make life easy and avoid writing these other Read* methods from scratch, we can disassemble the TextReader class using Reflector, and copy its versions of the other Read* methods, without having to change more than a few lines of code related to ArgumentExceptions.

The complete version of XmlSanitizingStream can be downloaded here. Rename the file extension to “.cs” from “.doc” after downloading.

kick it on DotNetKicks.com

89 Comments »

  1. Dan said

    I followed what the class (NoControlCharString) in your original post was doing, but not how to modify it based upon the method in your “Update: Sept. 16th, 2008”. Can you provide an updated class with your updated method? thanks

  2. Ono said

    Great solution! Exactly what I was looking for. Did some adaptations to use it to read the response of a badly constructed webservice and it worked perfectly. Thanks for the help.

  3. Tom said

    I converted some PDF content into a string, then was writing this string into an XML file when it balked at a “0x00” hex character. How can I use this to ‘scrub’ a string? Thanks.

  4. CAM said

    Great solution. Thanks for the assistance.

  5. Eran Kampf said

    You mentioned ASCII control characters 0×00 – 0×1F are not allowed in XML but I don’t see IsLegalXmlChar checking for those characters…

  6. Eran Kampf said

    Strike that.. my bad…

  7. Prakash said

    What about stripping out these invalid chars using Regex?
    “[\x00-\x1F]”

    • Remus said

      good call, now i can use visual studio to do it instead…

      thanks

  8. Michael said

    Thanks

  9. Tony Arnold said

    Wow! An excellent solution to a problem I have right now. The problem is, I’m not an experienced programmer (but I’m getting there each day!). Could I ask, without being laughed out of the room, how do I actually implement the above code? It’s a bit above my head, but it seems to resolve my problem (illegal xml characters) – but it seems a bit double dutch right now!

    Any help would be thankfully received.

    Cheers!

  10. rushikesh said

    Nice article …and very useful.

  11. Laurent Yin said

    I thought that this would be the solution to my problem, but actually, I had a slightly different problem.
    I used the XmlSerializer class of C# to serialize a class and it outputs in the file “&#x15;” (not a single character, but the xml code to represent it).
    And it still does the exception while trying to deserialize it.

    I’ve decided to use this regex to remove problematic parts, but it’s ugly…
    &#x(0?[0-8B-F]|1[0-9A-F]|7F);

    I don’t understand why .NET’s XmlSerializer would output these codes if it knows it won’t be able to read them while deserializing…

  12. Daron said

    I convert the CSharp Code into VB… Thanks for your original code.

    Public Function SanitizeXmlString(ByVal xml As String) As String

    If (xml Is Nothing) Then Throw New ArgumentNullException(“xml”)

    Dim buffer As StringBuilder = New StringBuilder(Xml.Length)

    Dim c As Char
    For Each c In xml
    If (IsLegalXmlChar(Microsoft.VisualBasic.AscW(c))) Then buffer.Append(c)
    Next

    Return buffer.ToString()
    End Function

    Public Function IsLegalXmlChar(ByVal character As Integer) As Boolean

    Return (character = Integer.Parse(“0x9”, Globalization.NumberStyles.HexNumber) _
    Or character = Integer.Parse(“0xA”, Globalization.NumberStyles.HexNumber) _
    Or character = Integer.Parse(“0xD”, Globalization.NumberStyles.HexNumber) _
    Or (character >= Integer.Parse(“0x20”, Globalization.NumberStyles.HexNumber) And character = Integer.Parse(“0xE000”, Globalization.NumberStyles.HexNumber) And character = Integer.Parse(“0x10000”, Globalization.NumberStyles.HexNumber) And character <= Integer.Parse(“0x10FFFF”, Globalization.NumberStyles.HexNumber)))
    End Function

  13. jman said

    I love you. The XmlSanitizingStream class has changed my life.

  14. Tony said

    Hi!

    This is a great article and it’s just what I need. I’ve looked at loads of other articles on the subject, but they really don’t come close to solving a very annoying habit. However, could you please tell me how to implement this class? I’ve tried but i don’t seem to be having any success. I would be really grateful for a little nudge in the right direction.

    many thanks,
    Tony

  15. Chris A said

    I don’t usually leave comments but this just cured a huge headache for me.I was having issues in pulling data out of Active Directory. The code works beautifully! Thanks.

  16. Sumit Mendiratta said

    The article was very helpful.

    Thanks
    Sumit Mendiratta

  17. T Kuznia said

    Thank you very much for this article.

    My Silverlight application was generating an invalid xml exception on the final character (0x00) when I was attempting to deserialize, and, after “cleaning up” the serialized xml string, everything works great.

    Thanks again.

  18. Kyle said

    Daron: That VB conversion won’t work, the two biggest problems being the hex conversions and the comparison isn’t the one listed (will strip valid characters)

    The corrected snippet:

    Public Shared Function StripInvalidXml(ByVal uncleanedXml As String) As String
    Dim cleanedXmlBuffer As New Text.StringBuilder(uncleanedXml.Length)

    For Each c As Char In uncleanedXml
    If IsLegalXmlCharacter(Microsoft.VisualBasic.AscW(c)) Then
    cleanedXmlBuffer.Append(c)
    End If
    Next

    Return cleanedXmlBuffer.ToString
    End Function

    Public Shared Function IsLegalXmlCharacter(ByVal character As Integer) As Boolean
    If (character = &H9 Or character = &HA Or character = &HD Or (character >= &H20 And character = &HE000 And character = &H10000 And character <= &H10FFFF)) Then
    Return True
    Else
    Return False
    End If
    End Function

    • msimmons said

      Kyle, I still cant make this work for me. Its stripping everything but the returns.

      • Henrik said

        Here is the correct one:

        Public Shared Function StripInvalidXml(ByVal uncleanedXml As String) As String
        Dim cleanedXmlBuffer As New Text.StringBuilder(uncleanedXml.Length)

        For Each c As Char In uncleanedXml
        If IsLegalXmlCharacter(Microsoft.VisualBasic.AscW(c)) Then
        cleanedXmlBuffer.Append(c)
        End If
        Next

        Return cleanedXmlBuffer.ToString
        End Function

        Public Shared Function IsLegalXmlCharacter(ByVal character As Integer) As Boolean
        If (character = &H9 Or character = &HA Or character = &HD Or (character >= &H20 And character = &HE000 And character <= &HFFFD) Or (character = &H10000 And character <= &H10FFFF)) Then
        Return True
        Else
        Return False
        End If
        End Function

  19. Rash said

    Thank you so much for this artical .I was getting the same error when i pull my data from the server.I found your artical after searching for a long time.And now i am using your methods and it all working absolutly fine.

    Thanks Again.

    Rash

  20. Debra Miller said

    Thanks!! (I am using the VB version)

  21. tulasi said

    Hi,
    I too Love you..

  22. Simon said

    Splendid, exactly what i was looking for, added to my util library

  23. Tim said

    I had this issue. In my case it was simply because the XML data originated from an HTTP post, whose body was retrieved as follows:

    ‘ Allocate buffer for the XML data.
    buf = New Byte(context.Request.InputStream.Length) {}

    ‘ Fill buffer with XML data.
    context.Request.InputStream.Position = 0
    context.Request.InputStream.Read(buf, 0, buf.Length)

    The bug was the byte array was one byte too long, which left a 0x00 character at the end. The correct declaration is

    buf = New Byte(context.Request.InputStream.Length – 1) {}

    Perhaps this is a common bug and cause of the invalid character error.

  24. Steve Plowman said

    You are a lifesaver. This is exactly what I was looking for!

  25. Andreas Warberg said

    Hi,

    Thanks for the article.

    I wanted to add that I have overcome this issue just using framework functionality.

    Below are methods that load and save an XDocument from and to a stream resp., without getting this exception:

    public static XDocument SafeLoad(Stream input)
    {
    var settings = new XmlReaderSettings { CheckCharacters = false };

    using (var textreader = new XmlTextReader(input))
    {
    var reader = XmlReader.Create(textreader, settings);
    return XDocument.Load(reader);
    }
    }

    public static void SafeSave(XDocument doc, Stream output)
    {
    var settings = new XmlWriterSettings { CheckCharacters = false };

    using (var textwriter = new XmlTextWriter(output, null))
    {
    var writer = XmlWriter.Create(textwriter, settings);
    doc.Save(writer);
    }
    }

    The key is the CheckCharacters property.

    @Tim: Maybe the issue you are facing comes simply from the way you are reading the stream. Here is a suggestion for how to extract the byte array from a stream:

    public static byte[] ReadToEnd(Stream input)
    {
    MemoryStream output;

    using (output = new MemoryStream())
    {
    input.CopyTo(output);
    }

    return output.ToArray();
    }

    Note that Stream.CopyTo is .NET 4 functionality.

    Also (since it is not clear whether you do this our not from your example) remember to apply the using-pattern to your streams so that they will always be properly disposed off (closed).

    Not disposing the streams can cause a number of hard-to-debug issues, I’ve learned the hard way.

    Best regards,
    Andreas Warberg

    • Andreas Warberg said

      Below are simplified versions of SafeLoad and SafeSave:

      private static XDocument SafeLoad(Stream input)
      {
      var settings = new XmlReaderSettings { CheckCharacters = false };
      using (var reader = XmlReader.Create(input, settings))
      {
      return XDocument.Load(reader);
      }
      }

      private static void SafeSave(XDocument doc, Stream output)
      {
      var settings = new XmlWriterSettings { CheckCharacters = false };
      using (var writer = XmlWriter.Create(output, settings))
      {
      doc.Save(writer);
      }
      }

      One thing to keep in mind that while the solution by the author of this blog removes the invalid characters, my proposal merely enables .NET to not choke and halt when invalid characters are encountered — they are not removed from the XML document (hence the need for the SaveSave method).

      If you intend on sending the XML downstream to another service eg. you might want to take a stand and clean up the XML. My proposal merely passes along the problems, more or less.

      Best regards,
      Andreas Warberg

  26. David said

    Awesome! This solves it! Thanks!

  27. santosh C said

    Hey Thanks for the post, I have a question here.

    I don’t think these all characters are not allowed in XML, there must be some way around it.
    Why can’t we use an different encoding method for XML file.
    I guess, UTF-8 only supports ASCII characters from 0 – 127.
    So, In order to use other characters we need to use UTF-16 encoding.

    Please advice, and correct me if I am wrong.

    • Chris said

      Hey santosh,

      XML is a specification. If you leave in any characters that are scrubbed out by XmlSanitizingStream, the data is not XML. It may be XML-like, but strictly speaking (and technology interoperability is all about agreeing on standards and specifications), it’s not XML.

      You can use whatever encoding you want to store XML data. You could store it in binary. You could store it as a UTF-8-encoded text file. But it’s not XML until it has been transformed (e.g. decoded) to into a representation that does not violate the specification.

      You’re free to add in or leave in any characters that XmlSanitizingStream removes, but then the data won’t represent XML. Furthermore, if the BCL or platform you use to parse the XML is strictly compliant with the 1.0 or 1.1 specifications, said XML data containing illegal characters should not be interpretable as XML. If your platform allows you to treat such data as XML when it contains illegal characters, it’s either covering up the fact that the data is malformed and correcting it for you, or it’s ignoring the specification and not implemented correctly.

      Either way, you’re doing yourself a disservice by not invalidating XML that contains illegal characters. What if you parse XML containing illegal characters on one platform, and that platform is smart enough to recover and continue, ignoring those characters, and then that same data is transferred to or used by another system that obeys the specifications explicitly, and fails? This is why .NET throws an exception when illegal characters are found, as it should.

      • Santosh said

        Chris,
        Thanks for the advice.
        However here is what I want to share:

        If we can change the UTF-8 to UTF-16 is there any +ve impact?
        Can we able to add non-ascii characters in it?

  28. Josh said

    I’ve downloaded .doc file and converted it to a .cs file. Typically, when I create .cs files Visual Web Developer adds them to the App_Code file. Here are my questions…

    Where do I add the .cs file in my website’s directory?

    Do I simply add your xmlsanitizingstream.cs to the App_Code file and I’m done?

    Do I need to make reference to your .cs file in my code behind for the page that’s getting the “invalid character” error?

    Do I copy and paste your methods in the code behind for the page that’s getting the “invalid character” error?

  29. Simon Bridge said

    I’m certain, at one point, that I found a method in the .NET framework that actually came back with a list of these characters in char array, (much like System.IO.Path.GetInvalidPathChars()) but now I can’t find it to save myself.

    Have I gone instane, or does anyone else know of this?

    • Simon Bridge said

      I’m an idiot, it was my own code.

      Still, this might prove useful to someone out there:

      ///
      /// get a list of invalid xml characters.
      ///
      ///
      public static Char[] GetInvalidXMLChars()
      {
      List list = new List();
      // 1-31 except 9, 10, 13
      for (int i = 1; i <= 31; i++)
      {
      if (i != 9 && i != 10 && i != 13)
      list.Add((char)i);
      }
      return list.ToArray();
      }

  30. http://www.theplancollection.com/house-plan-related-articles/hexadecimal-value-invalid-character

    How to remove the invalid characters using a regular expression.

  31. Alex said

    Heya, thanks for the code snippet. I was wondering if anyone had tried XmlConvert.IsXmlChar(char), as it seems that does the same thing.

    http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.isxmlchar.aspx

    Thanks

    • jokecamp said

      Good idea. This seems to be working for me.

      return new string(text.Where(XmlConvert.IsXmlChar).ToArray());

  32. Alex said

    In reference to my previous comment, it seems IsXmlChar was added in .NET 4.0, so if you are using the most recent version of the framework it would appear to be a useful alternative.

  33. benoit said

    Hi,

    thanks for your code.
    You have a bug in line 67:
    (“xmlVersion”, string.Format(“‘{0}’ is not a valid XML version.”));

    it should be:
    (“xmlVersion”, string.Format(“‘{0}’ is not a valid XML version.”, xmlVersion));

    Thanks again for this solution.

  34. Michael Kropat said

    The XmlSanitizingStream code has a bug that causes it to filter out (valid) characters with a Unicode codepoint >= 0x10000. Take the following example (in LINQPad) which tries to sanitize a G Clef character (= U+1F3BC):

    using (var data = new MemoryStream(Encoding.UTF8.GetBytes(“\uD834\uDD1E”)))
    using (var sanitized = new XmlSanitizingStream(data))
    sanitized.ReadToEnd().Length.Dump();

    The length of the string is 0, but it should be 2. Don’t feel bad, I wrote code for the same purpose and it had the exact same bug with handling “surrogate pairs”. =) You can see how I solved this issue here:

    http://stackoverflow.com/questions/157646/best-way-to-encode-text-data-for-xml/732135#732135

  35. […] Hexadecimal value 0x is an invalid character […]

  36. Bharat S said

    Hi all, I have removed invalid xml characters but while loading the sanitized string using m_xmld.LoadXml(strSanitizedXML) throws an error “Root element is missing”. Any idea?

    – Bharat S

  37. Naga Vara Prasad said

    try with this
    .Replace(“” + (char)0x8 + “”, “”);

    This will replace 
    Hexadecimal in C#

  38. Steven Rasmussen said

    Thank You!!! This worked like a charm for me!

  39. ro said

    I got an exception deserializing XML because of an invalid control character. I saw this article but removing invalid characters was not an option for me because my requirement was to have the exact same string in both databases so i used what is described here:

    http://weblogs.asp.net/rajbk/archive/2009/10/04/xmlserializer-and-invalid-xml.aspx

    See the comment:
    Alternatively, you could use an XmlReader with CheckCharacters set to false to deserialize:

    private static T DeSerializeObject(string xml)

    {

    var settings = new XmlReaderSettings { CheckCharacters = false };

    using (var sr = new System.IO.StringReader(xml))

    using (var reader = XmlReader.Create(sr, settings))

    {

    var serializer = new XmlSerializer(typeof(T));

    return (T)serializer.Deserialize(reader);

    }

    }

  40. […] know that it is because of invalid charatercs in XML and it needs to be removed. I did look at this for removing invalid […]

  41. Bryan said

    instead of copying functions directly from xmltextreader, why not inherit directly from it instead of streamreader.
    then just have a private StreamReader member that gets initialized on construction, and everywhere you call base.read or base.peek, call streammember.read or streammember.peek. This way, you don’t have to rely on copy and pasted code from .NET internals

  42. Reblogged this on a record of a developer and commented:
    Really handy, saved me writing one myself for my XML Importer in SSIS

  43. Joe said

    You are a life saver!! Thank you so much!

  44. himanshupareek66 said

    Solution of my problem. Thanks.

  45. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
    is added I get three emails with the same comment.

    Is there any way you can remove me from that service? Thanks!

  46. […] XML. Il file rss di wordpress viene creato con dei caratteri non validi per le specifiche XML.Qui trovate un articolo che spiega meglio il pattern […]

  47. Thank you so much for giving everyone remarkably spectacular opportunity to check tips from this blog.
    It can be so

    pleasing and also jam-packed with amusement for me personally and my
    office

    acquaintances to visit your website more

    than thrice a

    week to find out the latest items you will have. And indeed,

    I am also always

    contented with your dazzling inspiring ideas you serve. Selected 1

    points in this article are particularly the best we’ve had.

  48. Outstanding post but I was

    wanting to know if you could write a litte more on
    this topic? I’d be very thankful if you

    could elaborate a little bit further. Kudos!

  49. Glenda said

    Have you ever thought about adding a little bit more than
    just your articles? I mean, what you say

    is valuable and all. However just imagine if you added some great visuals or videos to give your posts more,

    “pop”! Your content is excellent but with images and video clips,
    this website could

    definitely be one of the greatest in its field. Great blog!

  50. Great post however , I was wondering if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit further. Appreciate it!

  51. Troy said

    This web site really has all of the information
    I needed about this subject and didn’t know who to ask.

  52. gokulraj said

    The above code is not working for the char 0×03. Any ideas !

    • gokulraj said

      Sorry about that. It is working fine. I made some mistake, so couldn’t get proper result. I corrected it now and it is working fine.

  53. Raymond said

    Here is my question: I am using the XmlReader.Read function on my “form load” event. How would I get this to be incorporated into my code?

    string[] fileEntries = Directory.GetFiles(path, “*.xml”);

    foreach (string fileName in fileEntries)
    {
    XmlReader xr = XmlReader.Create(fileName, settings); //read XML folder

    while (xr.Read())
    {

  54. Miguel said

    I solve the problem in VB.NET with

    StringVariable.Replace(Chr(0), String.Empty)

  55. Matt said

    Thanks! This helped tremendously.

  56. […] However, this can not be ignored and needs to be handled somehow. So my search led me to this link and the copy of content is below just in case if the link disappears. Thanks to the author of this […]

  57. H said

    Very helpful.

  58. like said

    If some one wants to be updated with most recent technologies
    afterward he must be visit this site and be up to date everyday.

  59. […] Hexadecimal value 0x is an invalid character | foreach(bill … – Ever get a System.Xml.XmlException that says: “Hexadecimal value 0x[whatever] is an invalid character” …when trying to load a XML document using one of the .NET XML API objects like XmlReader, XmlDocument, or XDocument? […]

  60. […] Hexadecimal value 0x is an invalid character | foreach … – Sep 11, 2008 · Ever get a System.Xml.XmlException that says: “Hexadecimal value 0x[whatever] is an invalid character” …when trying to load a XML document using …… […]

  61. […] Hexadecimal value 0x is an invalid character | foreach … – Sep 11, 2008 · Ever get a System.Xml.XmlException that says: “Hexadecimal value 0x[whatever] is an invalid character” …when trying to load a XML document using one …… […]

  62. EMO said

    Thanks so much, I needed a solution exactly like this and was in too much of a hurry to write it myself. This is the way to go when processing Gigabyte XML files. Thanks again.

  63. […] Hexadecimal value 0x is an invalid character | foreach … – Sep 11, 2008 · Ever get a System.Xml.XmlException that says: “Hexadecimal value 0x[whatever] is an invalid character” …when trying to load a XML document using …… […]

  64. Girish M said

    I found the code you wrote here useful.I would like to know if you are OK with me using it without restriction, for example, under MIT licensing? If not, what licensing applies.

  65. Albert K said

    Thank you
    Great help.

  66. […] As an example:“Hexadecimal value 0x[whatever] is an invalid character”To check what is valid and invalid, please check quickly the link below:https://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/ […]

  67. In each case, the included (or advisable) software was used to check the camera’s performance, though there are many webcam software program alternate options.

  68. […] have an XML with invalid hexadecimal characters. I’ve read this, this and this and any other links given but failed to make it […]

  69. How to add a new button to firefox navigation toolbar?

  70. Arun said

    hi this arun

  71. Arun said

    hi this arun.’L’hexadecimal value 0x03 is an invalid character. line 1 position 638 how to salve PDF

  72. Arun said

    this is EPSON Pdf file

  73. mehong said

    awesome post!

  74. Soufiane Toumlal said

    awesome post!
    I try your solution but in my case StreamReader cannot detect encoded characters like &#x2;

    this is the XML part

    BarcodeCapture_0
    SimpleIndex
    DataMatrix
    DEA&#x2;”&#x1;¡CV°)Ñ &#x2;võ Fƒ´ 20100410050

  75. […] something as described in this blog post be […]

  76. […] https://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/ […]

  77. n3wjack said

    An old post, but still saving butts I see. Thanks for sharing. :D

  78. […] something as described in this blog post be […]

  79. […] As an example:“Hexadecimal value 0x[whatever] is an invalid character”To check what is valid and invalid, please check quickly the link below:https://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/ […]

RSS feed for comments on this post · TrackBack URI

Leave a reply to Caleb Troxler Cancel reply