The values in the XML contain invalid characters. For XML in general you must escape the less than sign and the ampersand like so: < and &
declare @xml varchar(max)
declare @hDoc int
set @xml = '<transaction>
<item itemId="1" value="Hello World" />
<item itemId="2" value="Hello &World" />
<item itemId="3" value="Hello <World" />
<item itemId="4" value="Hello >World" />
</transaction>'
exec sp_xml_preparedocument @hDoc OUTPUT, @xml
select
itemId
, value
from
openxml(@hDoc, '/transaction/item')
with (
itemId int,
value varchar(max)
) item
However when using openxml certain values won't work in general, specifically that curly apostrophe. I'm not sure what values are invalid, but I know that is one of them. So the solution is to use the native XML type in SQL Server 2005.
declare @xml xml
set @xml = '<transaction>
<item itemId="1" value="Hello World" />
<item itemId="2" value="Hello &World" />
<item itemId="3" value="Hello <World" />
<item itemId="4" value="Hello >World" />
<item itemId="5" value="Hello ’World" />
</transaction>'
select
item.value('@itemId', 'int')
, item.value('@value', 'varchar(max)')
from @xml.nodes('/transaction/item') [transaction](item)