通过存储过程传递带有特殊字符的XML时出错

时间:2015-07-25 13:30:59

标签: c# sql-server xml stored-procedures

我有一个包含特殊字符的XML文件,例如:

öüüß

当我尝试执行一个存储过程时出错,这是为了用xml字段值填充数据库。

我得到的第一个错误是:

XML parsing: line 55, character 117, well formed check: undeclared entity

这是检测到特殊字符的位置。现在,在研究了DTD后,我将其添加到xml文件中:

<!DOCTYPE myData [
 <!ENTITY auml    "&#228;" >
 <!ENTITY ouml    "&#246;" >
 <!ENTITY uuml    "&#252;" >
 <!ENTITY szlig   "&#223;" >
 ]>

传递更新的xml文件后,我收到另一个错误。 这是错误:

Parsing XML with internal subset DTDs not allowed. Use CONVERT with style option 2 to enable limited internal subset DTD support.

有没有办法通过C#处理此异常,修改xml文件以便sql server 2014可以处理特殊字符?或者其他任何方式?

这是我正在使用的存储过程:

USE [AFFILICAT]
GO
/****** Object:  StoredProcedure [dbo].[InsertXML]    Script Date: 25-07-    2015 18:27:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger: XML_Import.sql|0|0|C:\Users\nikhilbanerjee\Documents\SQL Server Management Studio\XML_Import.sql
ALTER PROCEDURE [dbo].[InsertXML]
@xml XML

AS
BEGIN
  SET NOCOUNT ON;

  INSERT INTO Programs
  SELECT
  ProgramsStore.value('(description/text())[1]','NVARCHAR(MAX)') AS description, --TAG
  ProgramsStore.value('(descriptionLocal/text())[1]','NVARCHAR(MAX)') AS descriptionLocal, --TAG
  ProgramsStore.value('(products/text())[1]','INT') AS products, --TAG
  ProgramsStore.value('(vertical/text())[1]','NVARCHAR(50)') AS vertical, --TAG
  ProgramsStore.value('(regions/text())[1]','NVARCHAR(MAX)') AS regions, --TAG
  ProgramsStore.value('@Id','INT') AS ID, --ATTRIBUTE
  ProgramsStore.value('(Name/text())[1]','VARCHAR(100)') AS Name, --TAG
  ProgramsStore.value('(AdRank/text())[1]','FLOAT') AS AdRank, --TAG
  ProgramsStore.value('(startDate/text())[1]','DATE') AS startDate, --TAG
  ProgramsStore.value('(url/text())[1]','NVARCHAR(MAX)') AS url, --TAG
  ProgramsStore.value('(image/text())[1]','NVARCHAR(MAX)') AS image, --TAG
  ProgramsStore.value('(currency/text())[1]','NVARCHAR(50)') AS currency, --TAG
  ProgramsStore.value('(status/text())[1]','NVARCHAR(10)') AS status, --TAG
  ProgramsStore.value('(terms/text())[1]','NVARCHAR(MAX)') AS terms, --TAG
  ProgramsStore.value('(returnTimeLeads/text())[1]','INT') AS returnTimeLeads, --TAG
  ProgramsStore.value('(returnTimeSales/text())[1]','INT') AS returnTimeSales, --TAG
  ProgramsStore.value('(policies/text())[1]','NVARCHAR(MAX)') AS policies, --TAG
  ProgramsStore.value('(categories/text())[1]','NVARCHAR(MAX)') AS categories --TAG

  FROM
  @xml.nodes('/SearchProgramsResponse/programItems/programItem')AS TEMPTABLE(ProgramsStore)
END

这是我的sql连接和sqlcommand执行:

        string xml = File.ReadAllText("Programs_modified.xml");

        WebUtility.HtmlEncode(xml);

        try 
        {
            using (SqlConnection con = new SqlConnection("Server=NIKHIL;Database=AFFILICAT;Trusted_Connection=true"))
            {
                using (SqlCommand cmd = new SqlCommand("InsertXML"))
                {
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@xml", xml);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        catch(SqlException e)
        {
            string exc = e.Message;
        }

0 个答案:

没有答案