ado.net - Read XML from SQL Server using OleDbDataReader -
i'm stuck trying read xml data sql server using oledb.
private static void main(string[] args){ var con = new oledbconnection("provider=sqlncli11.1;data source=localhost;integrated security=sspi;initial catalog=temp"); var cmd = new oledbcommand( "select [id] ,[description] [temp].[dbo].[sometable] [id]= 1 xml path, root('root')", con); con.open(); byte[] result = null; oledbdatareader reader = cmd.executereader(commandbehavior.closeconnection); while (reader.read()){ result = (byte[]) reader[0]; } memorystream stream = new memorystream(result); stream.position = 0; xmldocument doc = new xmldocument(); doc.load(stream); console.out.writeline(doc.outerxml); }
it fails saying data malformed. if convert byte array string see lot of "strange " characters. i'm doing wrong?
since result direct xml believe facing issue.you need result in row-set instead of scalar. read string, use loadxml instead of stream.
below code changed.
private static void main(string[] args) { var con = new oledbconnection("provider=sqlncli11.1;data source=localhost;integrated security=sspi;initial catalog=temp"); var cmd = new oledbcommand( "select (select [id] ,[description] [temp].[dbo].[sometable] [id]= 1 xml path, root('root')) xml", con); con.open(); string result = string.empty; oledbdatareader reader = cmd.executereader(commandbehavior.closeconnection); while (reader.read()) { result = reader[0].tostring(); } con.close(); xmldocument doc = new xmldocument(); doc.loadxml(result); console.out.writeline(doc.outerxml); }
Comments
Post a Comment