C# 关于将xdocument保存到sql server xml列

C# 关于将xdocument保存到sql server xml列,c#,sql-server,xml,linq-to-xml,C#,Sql Server,Xml,Linq To Xml,这是我的xml文件: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mfisoft.ru/soap" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www

这是我的xml文件:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mfisoft.ru/soap" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:selectRowsetResponse>
      <result SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
        <item xsi:type="ns2:Map">
          <item>
            <key xsi:type="xsd:string">report_id</key>
            <value xsi:type="xsd:string">246</value>
          </item>
        </item>
      </result>
    </ns1:selectRowsetResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
当我在SQLServerManagementStudio中编写这样的sql命令时

insert into HowToSaveXML values (5,'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mfisoft.ru/soap" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:selectRowsetResponse>
      <result SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
        <item xsi:type="ns2:Map">
          <item>
            <key xsi:type="xsd:string">report_id</key>
            <value xsi:type="xsd:string">246</value>
          </item>
        </item>
      </result>
    </ns1:selectRowsetResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>')
其中,sDocumentToXML.xml包含与上述文件相同的内容,但会引发异常:

Incorrect syntax near '<'.
The label 'xmlns' has already been declared. Label names must be unique within a query batch or stored procedure

它还会引发异常。

要将字符串值发送到SQL,您始终需要使用参数,而不是将查询构建为字符串

sqlCmdUpdate = new SqlCommand("insert into HowToSaveXML values (5 , @xml)", conn);
sqlCmdUpdate.Parameters.AddWithValue("@xml", xd.ToString());
文件:

Incorrect syntax near '<'.
The label 'xmlns' has already been declared. Label names must be unique within a query batch or stored procedure
    sqlCmdUpdate = new SqlCommand("insert into  HowToSaveXML values ( 5 , '" + xd.ToString() + "'", conn);
sqlCmdUpdate = new SqlCommand("insert into HowToSaveXML values (5 , @xml)", conn);
sqlCmdUpdate.Parameters.AddWithValue("@xml", xd.ToString());