ここに私の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>
これはSQLサーバーの私のテーブルです
CREATE TABLE [dbo].[HowToSaveXML](
[ID] [int] NOT NULL,
[XMLcontent] [xml] NOT NULL
)
SQLサーバー管理スタジオでそのような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>')
正常に動作し、テーブル HowToSaveXML に 1 つのレコードを正常に追加します
しかし、C#コードを使用して同じことを行うと、次のようになります:
XDocument xd = XDocument.Load(@"D:\temp\0919\sDocumentToXML.xml");
sqlCmdUpdate = new SqlCommand("insert into HowToSaveXML values ( 5 , " + xd.ToString(), conn);
sqlCmdUpdate.CommandTimeout = 200;
if (conn.State == ConnectionState.Closed)
conn.Open();
try
{
sqlCmdUpdate.ExecuteNonQuery();
}
catch (Exception aep)
{
}
sDocumentToXML.xml には上記のファイルと同じコンテンツが含まれていますが、例外がスローされます。
Incorrect syntax near '<'.
The label 'xmlns' has already been declared. Label names must be unique within a query batch or stored procedure
システムはノードの重複を許可していないようですが、管理スタジオでinsertコマンドを直接使用して同じxmlを正常に挿入できました。その理由は何ですか?c#コードを使用して同じことを行うにはどうすればよいですか? ありがとう。
プラス:このようにC#コードを変更した場合
sqlCmdUpdate = new SqlCommand("insert into HowToSaveXML values ( 5 , '" + xd.ToString() + "'", conn);
また、例外をスローします。