I want to read lots of data(single column nvarchar(max)) from SQL Server 2005 and deserialize it to an object. We are currently using the following, but this is not fast enough is there a better/efficient way to do it?
using(MemoryStream stream = Encoding.UTF8.GetBytes((string)cmd.ExecuteScalar()))
{
XmlTextReader xmlReader;
DataContractSerializer deserializer;
deserializer = new DataContractSerializer(typeToDeserialize);
xmlReader = new XmlTextReader(stream);
return deserializer.ReadObject(xmlReader);
}
I've als tried to do it with an SqlDataReader and GetBytes but then I get exceptions.
Thanx in advance.
Do you have the option of switching to use the
XMLdatatype? It stores the data in a binary format, and exposesXMLresults as anXmlReaderinstance. You're parsing the data as you read it from the database, but if you used anXMLcolumn, it would already be parsed.In the meantime, try something like this:
XmlTextReaderanymore.