我正在尝试返回 XML 输出的查询,其中属性值之一为 XML,没有 <,/> 编码。
示例:
<Event EventLogId="124018" EventCategoryCode="ABC"
EventTypeCode="ERROR"
xmlObject="<attributes><attribute>A1</attribute></attributes>" />
<Event EventLogId="124019" EventCategoryCode="DEF"
EventTypeCode="Warning"
xmlObject="<attributes><attribute>A2</attribute></attributes>" />
我尝试了这个查询
SELECT
EventLogId AS EventLogId
,EventCategoryCode AS EventCategoryCode
,EventTypeCode AS EventTypeCode
,CAST(Event.xmlObject AS NVARCHAR(MAX)) as xmlObject
FROM
EventLog Event (NOLOCK)
FOR XML AUTO
但是我得到了这个输出
<Event EventLogId="124018" EventCategoryCode="ABC"
EventTypeCode="ERROR"
xmlObject="<attributes><attribute>A1</attribute></attributes>" />
<Event EventLogId="124019" EventCategoryCode="DEF"
EventTypeCode="Warning"
xmlObject="<attributes><attribute>A2</attribute<</attributes>" />
我要<
, />
代替<
和>
最佳答案
It is not valid to have non-escaped (i.e. not entity encoded) characters in attributes or anywhere in XML.
为了 100% 准确,在我收到大量评论之前,您可以使用特殊的 DATA 语法,但这在属性中是不合法的。而且这个功能很少被使用。
您可以在此处阅读该标准 ( http://www.w3.org/TR/xml/ ) 基本 XML 标准实际上非常短,阅读起来应该不到一个小时。
关于sql - 在 SQL 中,如何形成 XML 输出,其中属性值之一为 XML,无需 <,> 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32503320/