Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Dataset compression


    private static byte[] DataSetCompress(DataSet dataSet)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();
        DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
        bf.Serialize(ds, dataSet);
        ds.Flush();
        ds.Close();
        return ms.ToArray();
    }
    private static DataSet DataSetDecompress(byte[] data)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(data);
        DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
        DataSet dataSet = (DataSet)bf.Deserialize(ds);
        return dataSet;
    }

Output DataSet as XML

I was having problems with my ajax lookups when the database fields returned weird characters like the copyright symbol. The responseXML would be blank. After many searches I found you need to output in utf-8 and add an xml declaration to the output that says it is utf-8. Here is the code below in vb.net for asp.net v1.1

   Private Sub OutputDataSetAsXML(ByRef dsSource As System.Data.DataSet)

      Dim xmlDoc As System.Xml.XmlDataDocument
      Dim xmlDec As System.Xml.XmlDeclaration
      Dim xmlWriter As System.Xml.XmlWriter

      ' setup response
      Me.Response.Clear()
      Me.Response.ContentType = "text/xml"
      Me.Response.Charset = "utf-8"
      xmlWriter = New System.Xml.XmlTextWriter(Me.Response.OutputStream, System.Text.Encoding.UTF8)

      ' create xml data document with xml declaration
      xmlDoc = New System.Xml.XmlDataDocument(dsSource)
      xmlDoc.DataSet.EnforceConstraints = False
      xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
      xmlDoc.PrependChild(xmlDec)

      ' write xml document to response
      xmlDoc.WriteTo(xmlWriter)
      xmlWriter.Flush()
      xmlWriter.Close()
      Response.End()

   End Sub 
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS