Illigal 限定名称字符,使用 C# 将 XML 解析为 SQL



我正在尝试使用具有XML解析的查询来过滤表中的列。此查询是用 C# 进行的,我正在使用 SQLDataAdapter 来执行查询。我的代码如下:

string sqlstring = $@"select * from [AS_GOV_PS_oud] inner join [AS_GOV_PStest13_1_2017v2]
                        on   
                                [AS_GOV_PStest13_1_2017v2].[I/O name]        = 'ST' where
cast('<a>' + replace([AS_GOV_PS_oud].Interconnection, '""', ' </ a >< a > ') + ' </ a > ' as xml).query('for $x in / a order by $x return string($x)').value(' / ', 'varchar(max)') =                            
cast('<a>' + replace([AS_GOV_PStest13_1_2017v2].Interconnection, '""', ' </ a >< a > ') + ' </ a > ' as xml).query('for $x in / a order by $x return string($x)').value(' / ', 'varchar(max)') ";
string cn = ConfigurationManager.ConnectionStrings["Scratchpad"].ConnectionString;
using (SqlConnection myConnection = new SqlConnection(cn))
            {
                SqlDataAdapter dataadapter = new SqlDataAdapter(sqlstring, myConnection);
            }

错误代码如下:

System.Data 中发生了类型为"System.Data.SqlClient.SqlException"的未处理异常.dll

其他信息:XML 分析:第 1 行,字符 33,非法限定名称字符

你的代码超级丑!

有了这个,你几乎打破了任何规则应该如何做到这一点。但是,您的问题仍然可以得到解答:空白在错误的地方

试试这个:

SELECT CAST('<a>test</a>' AS XML) --works
SELECT CAST('< a>test</a>' AS XML) --exception due to the blank in '< a>'
SELECT CAST('<a>test</ a>' AS XML) --exception due to the blank in '</ a>'

我的建议:再想一想整个事情...你真正想实现的目标是什么?使用这样的构造作为连接的条件是一种非常错误的方法......

重新考虑并放置一个新的查询,您可以在其中设置一些示例数据,描述您的需求并提供预期的输出。

放置一个指向此问题的链接,并且 - 如果您遵循这些建议 - SO 的舰队将冲进来并帮助您找到比这更好的解决方案......

最新更新