illegal xml character - how to find the offending row

  • Hello,

    I've been knocking my head against this one for a couple of hours. I'm getting this error:

    Msg 9420, Level 16, State 1, Line 21

    XML parsing: line 293, character 45, illegal xml character

    And I don't know what the row is that is having that problem, so I can fix the data or exclude the ID from the recordset.

    Usually I have been successful with the brute force approach of limiting the recordset by the value of IDs. But that's not working this time. I've also tried TRY/CATCH but the row and ID it returns don't throw the error when I select only that row. The error happens when I include the XML column in the SELECT or WHERE clause, but of course because the error doesn't happen when I exclude them I don't know which exact row is causing the error.

    Can anyone suggest a more comprehensive way to search for this offending record with the illegal XML character? I'll provide more code if needed.

    Thanks for any help!

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Check out this thread, http://www.sqlservercentral.com/Forums/Topic459044-21-1.aspx

  • Are you storing XML in a string datatype (varchar is the most common for that), and then trying to use an XML function on it or put it in an XML typed column/variable?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Jack Corbett (12/15/2011)


    Check out this thread, http://www.sqlservercentral.com/Forums/Topic459044-21-1.aspx

    Thanks so much. I tried using the function on that page but am still getting the error. I will try a few more variations of it, though.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • GSquared (12/15/2011)


    Are you storing XML in a string datatype (varchar is the most common for that), and then trying to use an XML function on it or put it in an XML typed column/variable?

    Thanks for your reply.

    There are two columns in this case. One is nvarchar(max) and the other is ntext. I think the offending character is in the nvarchar(max) column, but I can't seem to pin down the exact row. I do have a function that converts the data to XML, but it doesn't seem to stop the error. It may be the timing of when I'm trying to convert it, though (in the function), whereas the error happens if I add the column to the SELECT statement or the WHERE clause (implicit in the function call).

    I'll try a few more things then write back.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Your table has multiple rows of nvarchar(max), and one or more of those have an illegal XML character in them. Correct?

    Can you step through the table, assign that column to an XML-typed variable, and see which row(s) fail? A cursor with a Try Catch nested in it should be able to do that pretty rapidly, unless it's a LOT of data.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Another possibility occurs to me:

    Most "illegal XML character" errors I've run into have been ampersands ("&"). A few have been brackets. Since it's nvarchar(max), you can:

    select *

    from dbo.MyTable

    where MyNvarcharColumn like '%[%]%';

    That will give you rows that have an ampersand in the XML. Then you'll need to fix those rows, of course.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (12/15/2011)


    Another possibility occurs to me:

    Most "illegal XML character" errors I've run into have been ampersands ("&"). A few have been brackets. Since it's nvarchar(max), you can:

    select *

    from dbo.MyTable

    where MyNvarcharColumn like '%[%]%';

    That will give you rows that have an ampersand in the XML. Then you'll need to fix those rows, of course.

    Thanks again. Yes, I have tried a cursor -- I will post the code next after trying your search above.

    Interestingly, when I select only each column from the table along with the relevant id columns, I get no error. But when I encapsulate the column in a call to the function to extract the required XML element value, I get the error. I'm going to take a closer look at how the functions process the data and see where that leads.

    Thanks again,

    webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply