1
我有一個定義的XML列的SQL表如下:當我嘗試插入帶有類型化XML字段的SQL表時,爲什麼會得到「XML驗證:元素聲明未找到元素」?
ID int
FooType nvarchar(255)
FooXML xml(CONTENT dbo.FooXMLSchemaCollection)
架構(FooXMLSchemaCollection)定義爲:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:t="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema"
targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema"
elementFormDefault="qualified">
<xsd:element name="item">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="key">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="string" type="xsd:string" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="value">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="ArrayOfString">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="string" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
我想一些測試數據插入到表。 我試圖插入XML看起來是這樣的:
<item>
<key>
<string>Attributes</string>
</key>
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>fieldName1</string>
<string>fieldName2</string>
<string>fieldName3</string>
<string>fieldName4</string>
<string>fieldName5</string>
<string>fieldName6</string>
</ArrayOfString>
</value>
</item>}
我試圖通過SSMS以下到數據庫:
INSERT INTO tblLabelInfo
([ID] ,[FooType],[FooXML])
VALUES
(1, 'SampleInstance',
N'<?xml version="1.0" encoding="utf-16"?><item><key><string>Attributes</string></key><value><ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><string>fieldName1</string><string>fieldName2</string><string>fieldName3</string><string>fieldName4</string><string>fieldName5</string><string>fieldName6</string></ArrayOfString></value></item>')
我得到SSMS以下錯誤消息:
消息6913,級別16,狀態1,行
XML驗證:聲明未找到元素「項目'。位置:/ *:item [1]
我在這裏錯過/做錯了什麼?
感謝, JohnB