2014-10-08 67 views
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

回答

0

我發現,如果我包含在XML中的命名空間和使用命名空間的XSD從 引用的元素,我是能夠插入:

INSERT INTO tblFooInfo 
     ([FooType] , [FooXML]) 
    VALUES 
    ('Package3' 
    N'<?xml version="1.0" encoding="utf-16"?> 
    <t:item xmlns:t="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema"> 
    <t:key> 
     <t:string>Attributes</t:string> 
    </t:key> 
    <t:value> 
     <t:ArrayOfString > 
     <t:string>fieldName1</t:string> 
     <t:string>fieldName2</t:string> 
     <t:string>fieldName3</t:string> 
     <t:string>fieldName4</t:string> 
     <t:string>fieldName5</t:string> 
     <t:string>fieldName6</t:string> 
     </t:ArrayOfString> 
    </t:value> 
    </t:item>') 

問題,如果我通過使用 XmlWriter類創建xml字符串的.Net應用程序嘗試它,我該如何實現此目的?

相關問題