Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

illegal xml character - how to find the offending row Expand / Collapse
Author
Message
Posted Wednesday, December 14, 2011 3:50 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 2,328, Visits: 2,658
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #1222112
Posted Thursday, December 15, 2011 11:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:07 AM
Points: 11,157, Visits: 12,899
Check out this thread, http://www.sqlservercentral.com/Forums/Topic459044-21-1.aspx



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1222681
Posted Thursday, December 15, 2011 12:18 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
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
Post #1222704
Posted Thursday, December 15, 2011 1:17 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 2,328, Visits: 2,658
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #1222736
Posted Thursday, December 15, 2011 1:19 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 2,328, Visits: 2,658
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #1222738
Posted Thursday, December 15, 2011 2:12 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
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
Post #1222767
Posted Thursday, December 15, 2011 2:14 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
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
Post #1222768
Posted Thursday, December 15, 2011 2:28 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 2,328, Visits: 2,658
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #1222779
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse