【问题标题】:XmlReader not reading Xml result from stored procedureXmlReader 未从存储过程读取 Xml 结果
【发布时间】:2017-02-02 11:58:10
【问题描述】:

我有一个以 XML 格式返回数据的存储过程。最初根节点是表名本身。我还进行了更改,使根节点为“xml”。

在存储过程中:

SELECT * FROM mytable
FOR XML PATH('mytable'), ROOT('xml'), ELEMENTS;

读取结果时出现同样的问题,如下:

string xmlresult = string.Empty;

using (var command = (SqlCommand)connection.CreateCommand())
{
    command.CommandText = "sp_dosomething";
    command.CommandType = CommandType.StoredProcedure;

    using (XmlReader reader = command.ExecuteXmlReader())
    {
        while (reader.Read())
        {
            xmlresult = reader.ReadOuterXml();
        }
    }
}

例外是:

发送到 ExecuteXmlReader 的命令无效。该命令必须返回 Xml 结果。

在:

在 System.Data.SqlClient.SqlCommand.CompleteXmlReader(SqlDataReader ds) 在 System.Data.SqlClient.SqlCommand.ExecuteXmlReader()

我做错了什么?

【问题讨论】:

  • 请提供您的xml和存储过程。
  • @RonakPatel xml 和 sp 非常复杂,但我会添加返回 xml 的方式。
  • 您至少需要提供演示 xml 以便我可以帮助您。
  • mytable里面有什么类型的数据?
  • @DanielMinnaar 普通数据类型,如 int、varchar、bit、money。没有文本、图像或 xml 列。

标签: c# sql-server


【解决方案1】:

从错误中可以清楚地看出,您得到的不是 XML 结果。

不要使用 ExecuteXmlReader() 尝试使用 ExecuteReader() 来获取输出并查看。这绝对不是一个好的 XML。

// execute the command
reader = cmd.ExecuteReader();

// iterate through results, printing each to console
while (reader.Read())
{
    Console.WriteLine("Print the data here and check output will not be XML in your case");
}

【讨论】:

  • 我按照你的建议做了,但例外是:无法将“System.Guid”类型的对象转换为“System.String”类型。注意:xml 中的一些节点是uniqueidentifier。它仍然没有返回完整的 xml。
  • 简单的建议,指向正确的调试方向。
猜你喜欢
  • 2018-02-28
  • 2015-08-15
  • 2011-03-03
  • 2021-12-11
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2014-04-23
  • 2018-12-27
相关资源
最近更新 更多