1

I have heard that it might be possible to compress the returned data from a web method call. The web method returns a string, which my client application then uses to generate an xml document.

Here is a code snippet.

[WebMethod]
public string GetRegions()
{
    // Create a new string builder for which the XmlWriter will append to.
    StringBuilder output = new StringBuilder();

    // Create XmlWriter settings
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "    ";

    // Use an XmlWriter and a StringBuilder for outputting the xml.
    using (XmlWriter writer = XmlWriter.Create(output, settings))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("MyData");
        writer.WriteStartElement("Regions");

        // Get all selectable regions
        RegionList regionList = RegionList.GetSelectable();

        // Loop through every region
        foreach(Region region in regionList.GetList())
        {
            writer.WriteStartElement("Region");
            writer.WriteElementString("RegionId", region.regionID.ToString());
            writer.WriteElementString("Code", region.code);
            writer.WriteElementString("Name", region.name);
            writer.WriteEndElement();

        }

        writer.WriteEndElement();
        writer.WriteEndDocument();

    }
    // Return the xml as a string
    return output.ToString();
}

I've heard about adding something to the request headers e.g. "Accept-Encoding : gzip" but I am not sure how to get this to work. Does anyone know of a solution to my problem?

Thanks in advance.

3
  • 1
    You can enable compression on the virtual directory hosting the webservice in IIS Manager, then it will work out whether the client can accept compressed (GZIP) or not.
    – kpcrash
    Commented Mar 28, 2012 at 12:45
  • Related question: stackoverflow.com/questions/4377106/…
    – M.Babcock
    Commented Mar 28, 2012 at 12:47
  • Has this ever been resolved @bobbo ? I use compresison, but manually. I could share that if you still need to achieve something like this? Commented Nov 27, 2013 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.