views:

816

answers:

3

I get this error when reading some data from an SQL column then converting it to XML:

"System.InvalidOperationException: There is an error in XML document (182, 16). ---> System.Xml.XmlException: ' ', hexadecimal value 0x0B, is an invalid character."

Fair enough, maybe the data is malformed. Except, how can I find the culprit row?

SELECT * from Mytable where Column like '%' + char(0x0B)+'%'

returns empty.

(obviously I attempted all %+char , char, char+% combinations, just in case)

+2  A: 

Have you tried nchar(0x0B) instead of char(0x0B) yet? As it sounds like your dodgy character is Unicode.

Wim Hollebrandse
Good one ! Hadn't tried this yet.But it doesn't work. Still returns empty
Radu094
What datatype is the column?
Wim Hollebrandse
guilty Column is nvarchar(MAX)
Radu094
Wonder if it's something to do with collation. Have you tried with different COLLATE clauses?
Wim Hollebrandse
I also think COLLATE might be the problem, but what puzzles me is that the query WILL find a 0x0B character as long as it is in the first ~100 or so characters... very unusual!
Radu094
Never mind.. it will find 0x0B in any position as long as it is there. Turns out my bad row of data did not contain a 0x0B (see my answer)
Radu094
A: 

Character 0x0B cannot be used in an XML document (see the list of valid XML characters here.) Please consider migrating invalid XML characters like this to valid XML (e.g. the sequence ).

fbrereto
Well, the vertical tab got there thanks to http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=275603Problem now is how to find it and get it out
Radu094
A: 

Finally found it !

The .NET XML serializer was escaping the invalid character when serializing it, but then it was un-escaping it before de-serialization.

So I had to search for the escaped &#xB to find the un-escaped 0x0B ... seriously not funny guys!

So this:

  SELECT * from Mytable where Column like '%' + '&#xB' + '%'

Will actually find this:

<?xml version="1.0"?>
      <Hashtable><key>313_other_10</key><value>&#xB</value></Hashtable>

And while this looks like valid XML it will throw an invalid character exception when :

    XmlSerializer xs = new XmlSerializer(Type.GetType(Hashtable));
    StringReader stringReader = new StringReader(xml);
    obj = xs.Deserialize(stringReader);

Many thanks to people who jumped in to help! It was unvaluable help!

Radu094
Well, you hadn't said you were storing proper XML data in that column! That would have helped.I'd simply though you were loading normal text content into an XML document.
Wim Hollebrandse