今天在调试程序时,非常意外地得到了一个错误信息:
XML parsing: character 2033, unexpected end of input。
经过设置断点,终于发现了错误的所在。原来在项目数据库 SQL Server 2005中有这么一个 存储过程,如SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS,然后我把返回值存到 DataSet中,再给应用层调用。
但是问题就出在返回值这里,由于返回的是XML,那Fill到DataSet中的也就只有一列一行了,而现在因为返回值的长度超过了2033个字符,之后就被切割成2条Row了,那可想而知了,如果返回的是类似于“
发现问题那当然要解决问题了,这个问题还不小呢,因为我目前的这个项目很多都是要用到XML,如果都出现这样的错误,那后果就。。。。。。
难道要更改数据库字段吗?难道XML的返回值就只好是小于2033个字符吗???
于是我就搜索搜索再搜索,有篇老外的帖子不错(http://aspnetresources.com/blog/executescalar_truncates_xml.aspx),也提到了这个问题,但是他的解决方法似乎不是最好。他是使用 ExecuteXmlReader来解决的,代码如下:
connection.Open ();
rdr
=
command.ExecuteXmlReader ();
rdr.Read ();
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
while
(rdr.ReadState
!=
ReadState.EndOfFile)
{
result += rdr.ReadOuterXml ();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
rdr.Close ();
很显然是采用循环加拼接字符串解决的,貌似可以,但总觉得非常麻烦,也不太理想。
这时候功夫不负有心人啊,就在这篇帖子的Comments中,我意外地得到了答案,哈哈哈。
原文如下:
I can't explain this, but we found another way to get around the issue. My original stored procedure just selected XML results directly like this:
SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS
I changed it to select the XML results into an XML variable and then selected those to return and the truncation issue was gone.
DECLARE @xml XML
SET @xml = (SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS)
SELECT @xml
Not sure exactly why this makes a difference but this allows us to keep using the one line ExecuteScalar instead of looping, which is nice.
于是我就照葫芦画瓢,终于解决了问题。这样只需要修改存储过程就要可以了,不用再修改字段和代码了,看来不错。至于这样一个解决办法的原理,有空再研究了。
经过设置断点,终于发现了错误的所在。原来在项目数据库 SQL Server 2005中有这么一个 存储过程,如SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS,然后我把返回值存到 DataSet中,再给应用层调用。
但是问题就出在返回值这里,由于返回的是XML,那Fill到DataSet中的也就只有一列一行了,而现在因为返回值的长度超过了2033个字符,之后就被切割成2条Row了,那可想而知了,如果返回的是类似于“
<Tree>...</Tree>
”这样的XML,被切割成2条Row后,那当然出错了。参见下图(DataSet的值被切割)。
![](https://p-blog.csdn.net/images/p_blog_csdn_net/broze/error1.jpg)
发现问题那当然要解决问题了,这个问题还不小呢,因为我目前的这个项目很多都是要用到XML,如果都出现这样的错误,那后果就。。。。。。
难道要更改数据库字段吗?难道XML的返回值就只好是小于2033个字符吗???
于是我就搜索搜索再搜索,有篇老外的帖子不错(http://aspnetresources.com/blog/executescalar_truncates_xml.aspx),也提到了这个问题,但是他的解决方法似乎不是最好。他是使用 ExecuteXmlReader来解决的,代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
很显然是采用循环加拼接字符串解决的,貌似可以,但总觉得非常麻烦,也不太理想。
这时候功夫不负有心人啊,就在这篇帖子的Comments中,我意外地得到了答案,哈哈哈。
原文如下:
I can't explain this, but we found another way to get around the issue. My original stored procedure just selected XML results directly like this:
SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS
I changed it to select the XML results into an XML variable and then selected those to return and the truncation issue was gone.
DECLARE @xml XML
SET @xml = (SELECT * FROM tbl_Test FOR XML AUTO, ELEMENTS)
SELECT @xml
Not sure exactly why this makes a difference but this allows us to keep using the one line ExecuteScalar instead of looping, which is nice.
于是我就照葫芦画瓢,终于解决了问题。这样只需要修改存储过程就要可以了,不用再修改字段和代码了,看来不错。至于这样一个解决办法的原理,有空再研究了。