SqlServer Xml Data Type - How to Force Full End Elements
I have a table in SQL Server...
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[DataItemsXml] [xml] NULL,
which has an xml column. I want to store the xml in this column in the following format...
<data>
<item-1>Some Data Here</item-1>
<item-2></item-2>
</data>
Notice the empty element item-2. I need to store this in this manner to support some code I cannot modify.
Even when I pass the XML in the insert or update statement in this format, SQL Server appears to convert empty elements like item-2 to the following...
<data>
<item-1>Some Data Here</item-1>
<item-2 />
</data>
I understand that this is perfectly valid XML, but I need to have SQL Server store it in the first format, with full end 开发者_如何学Ctags for elements, even for empty elements.
I cannot figure out how to do this.
Any ideas? Thanks!
According to Microsoft:
SQL Server preserves the content of the XML instance, but does not preserve aspects of the XML instance that are not considered to be significant in the XML data model. This means that a retrieved XML instance might not be identical to the instance that was stored in the server, but will contain the same information.
I can't find any options that force SQL Server to expand elements as you wish.
If all you need is to store the XML rather than querying it, you could store it as a varchar(max)
field, which would avoid the reparsing. Alternatively, if it needs to be an actual xml
field, you could convert it to text when retrieving it, converting the compact representation to end-element form using string manipulation. Although it's probably theoretically possible to do such string manipulation in SQL Server, it's a much better idea to do it in a CLR procedure or in your application if you can.
精彩评论