简体   繁体   English

如何处理'sp_xml_preparedocument'中的无效字符(SQL Server 20188 R2)

[英]How to handle invalid character in 'sp_xml_preparedocument' (SQL Server 20188 R2)

I have a stored procedure where I am using sp_xml_preparedocument to handle XML data. 我有一个存储过程,正在使用sp_xml_preparedocument处理XML数据。 But due to some invalid characters like 但是由于某些无效字符,例如

Ex: 1. INGENIERÍA
    2. Engineer'

in XML data, SQL throws an exception as "An invalid character can be found in the text content". 在XML数据中,SQL引发异常,例如“在文本内容中可以找到无效字符”。

附加的异常的屏幕截图

My Stored procedure look like 我的存储过程看起来像

    DECLARE @idoc INT
    DECLARE @doc XML
    SET @doc = @DocElements -- @Docelements will have XML data
    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

    SELECT BadgeNo FROM OPENXML (@idoc, '/DocumentElement/PEScoreUpdate',2) WITH(BadgeNo  VARCHAR(50)))AND
    ASRYEAR=YEAR(GETDATE()) And IsPlanDeleted<>1
    EXEC sp_xml_removedocument @idoc 

Sampel XML data Sampel XML数据

  <DocumentElement>
  <PEScoreUpdate>
    <Badge_x0020_No>105731</Badge_x0020_No>       
    <Last_x0020_Name>Vijaya Kumar</Last_x0020_Name>
    <First_x0020_Name>Sanjay Kumar</First_x0020_Name>
    <BOC>Onshore E&amp;C</BOC>
    <Emp_x0020_Class>White Collar</Emp_x0020_Class>
    <Site>INGENIERÍA PROJECT     Secondment</Site>       
  </PEScoreUpdate>
</DocumentElement>

I am getting this XML data from an excel sheet with thousands of data so, it's practically not possible to search for invalid text and correct it manually. 我从具有数千个数据的excel工作表中获取此XML数据,因此,实际上不可能搜索无效文本并手动进行更正。 Anyone guide me how to handle this invalid char in SQL procedure. 任何人都指导我如何在SQL过程中处理此无效的char。 Is there any way to replace this invalid character but I am processing thousands of data. 有什么办法可以替换这个无效字符,但是我正在处理成千上万的数据。 will it affect performance? 会影响性能吗? Anyone guide me. 有人引导我。 Thanks in Advance 提前致谢

One point is, that FROM OPENXML (together with the SPs to prepare and remove a dcoument) is outdated and should not be used any more (rare exceptions exist). 有一点是, FROM OPENXML (与SP一起准备和删除商品)已经过时了,不应再使用(存在罕见的例外)。

To your actual question: 对您的实际问题:

I'm pretty sure, that this issue is not related to FROM OPENXML , neither to sp_xml_preparedocument . 我很确定,此问题与FROM OPENXML无关,也与sp_xml_preparedocument无关。 You did not show, how you are passing in the XML. 您没有显示如何传递XML。 But - assumeably - you do this on string-level with a VARCHAR variable (or with an XML-literal. Try to use NVARCHAR or place a leading N before you literal. 但是-可以想象-您是在字符串级使用VARCHAR变量(或使用XML字面量)执行此操作。尝试使用NVARCHAR或在字面量之前放置前导N

If I'm right, you find details here: https://stackoverflow.com/a/42683643/5089204 如果我说得对,您可以在这里找到详细信息: https : //stackoverflow.com/a/42683643/5089204

UPDATE How to read XML today... 更新如何今天阅读XML ...

Try to read your XML like this 尝试像这样读取您的XML

DECLARE @doc XML=
N'<DocumentElement>
  <PEScoreUpdate>
    <Badge_x0020_No>105731</Badge_x0020_No>       
    <Last_x0020_Name>Vijaya Kumar</Last_x0020_Name>
    <First_x0020_Name>Sanjay Kumar</First_x0020_Name>
    <BOC>Onshore E&amp;C</BOC>
    <Emp_x0020_Class>White Collar</Emp_x0020_Class>
    <Site>INGENIERÍA PROJECT     Secondment</Site>       
  </PEScoreUpdate>
</DocumentElement>';

SELECT u.value(N'(Badge_x0020_No)[1]',N'int') AS Badge_x0020_No
      ,u.value(N'(First_x0020_Name)[1]',N'nvarchar(max)') AS First_x0020_Name
      ,u.value(N'(BOC)[1]',N'nvarchar(max)') AS BOC
      ,u.value(N'(Emp_x0020_Class)[1]',N'nvarchar(max)') AS Emp_x0020_Class
      ,u.value(N'(Site)[1]',N'nvarchar(max)') AS [Site]
FROM @doc.nodes(N'/DocumentElement/PEScoreUpdate') AS A(u)

UPDATE 2 更新2

Your code shows /DocumentElement/BasicInformation as XPath , but this doesn't show up in your XML? 您的代码将/DocumentElement/BasicInformation显示为XPath ,但是这不会显示在XML中吗?

Furthermore, the _x0020_ within your elements names is coming from blanks in your Excel's columns names. 此外,元素名称中的_x0020_来自Excel列名称中的空白。

There are several places, where your issue might come from... 在很多地方,您的问题可能来自...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM