سؤال

I am not sure why, but our nLog configuration is now stored as an nvarchar(max) column in a table. With a standard SELECT on the column, I get output like:

<variable name="HeaderLayout".... 

I have tried using "FOR XML AUTO", as well as a series of REPLACE functions to get nice, pretty XML output from this column. Option A doesn't automatically cast the replacement markers to < or >, option B is really clumsy. Can someone please recommend a good why to retrieve the configuration as formatted XML?

As a bonus, I'd like to be able to take the formatted XML, make a change and then INSERT with a minimum of effort, but for now, I'd just settle for being able to make the SELECT results more readable.

Thank you!

هل كانت مفيدة؟

المحلول

First cast the value to XML. That will create an XML with your entire XML as a single value. Then you get that value from the XML using the value function and finally you cast the fetched value back to XML.

Here is some code using a table variable you can test on.

declare @T table
(
  Col nvarchar(max)
)

insert into @T(Col)
values('&lt;root&gt;&lt;item ID="1"&gt;textvalue&lt;/item&gt;&lt;/root&gt;') 

select cast(cast(T.Col as xml).value('.', 'nvarchar(max)') as xml) as XMLCol
from @T as T

Result:

XMLCol
-------------------------------------------
<root><item ID="1">textvalue</item></root>

نصائح أخرى

Try FOR XML AUTO, TYPE. And can you provide an example of your query?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top