Creé un Procedimiento almacenado que devuelve XML y también me gustaría devolver ese XML en un método que creé.¿Cómo devuelvo XML de un procedimiento almacenado?
Tengo dos problemas. En primer lugar, después de realizar algunas búsquedas, no se recomienda utilizar .ExecuteScalar();
porque trunca cadenas de más de 2033 caracteres.
Por lo tanto, me encontré con una función llamada ExecuteXMlReader()
, pero en Visual Web Developer 2010 Express que se ejecuta en .NET 4.0 (C#) es tirar el error "System.Data.SqlClient.SqlCommand' does not contain a definition for 'ExecuteXMlReader' and no extension method 'ExecuteXMlReader' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found"
Aquí está mi procedimiento almacenado:
CREATE PROCEDURE dbo.GETReport
(@ReportDate date)
AS
SELECT * FROM ReportTbl
WHERE ReportDate = @ReportDate
for xml auto, elements
set nocount on;
RETURN
Aquí es mi método:
using System.Data;
using System.Data.SqlClient;
...
//connect
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=foo; Password=foo; Initial Catalog=Database1");
conn.Open();
//create command
SqlCommand cmd = new SqlCommand("dbo.GETReport", conn);
cmd.Parameters.AddWithValue("@ReportDate", "3/24/2011");
cmd.CommandType = CommandType.StoredProcedure;
DataReader rd = cmd.ExecuteXMlReader(); //this is where error is occuring
//also, it is throwing an error for DataReader as well saying there is no
//type of namespace with that name
rd.Read();
string s = rd.ReadOuterXml(); //also dont know if this is how i should return the XML
en segundo lugar, además de la cuestión ExecuteXMLReader()
, no sé si el retorno de una string es la forma correcta de devolver XML en primer lugar ... ¿Hay otro tipo de objeto en el que debería convertirlo? ¿O otra función que debería usar?
Gracias de antemano!
que funcionó muy bien, pero una pregunta rápida ... ¿hay alguna manera de devolver el XML como XML y no como una cadena? – AngeloS
El 'XmlReader' es la forma subyacente de leer cualquier XML. Lo usaría para cargar un XDocument o XmlDocument, usando XDocument.Load (reader), por ejemplo. –
@Angelo, podría usar algo similar a 'XmlDocument document = new XmlDocument(); document.Load (reader); 'o use' XDocument.Load (reader) 'como se menciona en @John. Gracias, @John! – Alex