How do I escape double quotes in attributes in an XML String in T-SQL? How do I escape double quotes in attributes in an XML String in T-SQL? xml xml

How do I escape double quotes in attributes in an XML String in T-SQL?


Wouldn't that be " in xml? i.e.

"hi "mom" lol" 

**edit: ** tested; works fine:

declare @xml xml set @xml = '<transaction><item value="hi "mom" lol"     ItemId="106"  ItemType="2"  instanceId="215923801"  dataSetId="1" /></transaction>'select @xml.value('(//item/@value)[1]','varchar(50)')


tSql escapes a double quote with another double quote. So if you wanted it to be part of your sql string literal you would do this:

declare @xml xml set @xml = "<transaction><item value=""hi"" /></transaction>"

If you want to include a quote inside a value in the xml itself, you use an entity, which would look like this:

declare @xml xmlset @xml = "<transaction><item value=""hi "mom" lol"" /></transaction>"


Cannot comment anymore but voted it up and wanted to let folks know that " works very well for the xml config files when forming regex expressions for RegexTransformer in Solr like so: regex=".*img src="(.*)".*" using the escaped version instead of double-quotes.