locked
Storing whitespace-only values in tables RRS feed

  • Question

  • Hi,

      I have found that if I try to store only whitespace (e.g., spaces, new-lines, tabs, etc') in any of the fields of an Azure table, the whitespace is removed. If I include at least one non-whitespace character, the whitespace is retained. Is there any way to keep whitespace in all situtations?

    Thank you.

    // Example code:

    using System;
    
    using Microsoft.WindowsAzure;
    
    using Microsoft.WindowsAzure.StorageClient;
    
    
    
    namespace Test
    
    {
    
    
    
     class Program
    
     {
    
     
    
      static void Main(string[] args)
    
      {
    
    
    
       TableServiceContext context = initialise();
    
    
    
       Test test = new Test("x", "y");
    
       test.field = " \r \n \t ";
    
    
    
       Console.WriteLine(test.field.Length); // 7
    
       Console.WriteLine(test.field == " \r \n \t "); // True
    
    
    
       context.AddObject("Tests", test);
    
       context.SaveChanges();
    
    
    
       Console.WriteLine(test.field.Length); // 0
    
       Console.WriteLine(test.field == " \r \n \t "); // False
    
    
    
      }
    
    
    
      private static TableServiceContext initialise()
    
      { CloudStorageAccount.SetConfigurationSettingPublisher
    
       ( (configName, configSetter) =>
    
       { configSetter(configName);
    
       }
    
       );
    
       CloudStorageAccount storageAccount = CloudStorageAccount
    
       . FromConfigurationSetting
    
       ( "UseDevelopmentStorage=true"
    
       );
    
       storageAccount
    
       . CreateCloudTableClient()
    
       . CreateTableIfNotExist("Tests");
    
       return new TableServiceContext
    
       ( storageAccount.TableEndpoint.ToString()
    
       , storageAccount.Credentials
    
       );
    
      }
    
    
    
      class Test : TableServiceEntity
    
      {
    
    
    
       public Test()
    
       : base()
    
       { }
    
    
    
       public Test
    
       ( string partitionKey
    
       , string rowKey
    
       ): base
    
       ( partitionKey
    
       , rowKey
    
       ) { }
    
    
    
       public string field
    
       { get;
    
       set;
    
       }
    
    
    
      }
    
    
    
     }
    
    
    
    }
    
    
    
    
    Friday, February 11, 2011 3:12 PM

Answers

  • OK - I think I get it.  I would suggest two alternatives; the first one is to transform your characters into a byte array; that way everything will be stored "as-is".  The other one is to URL Encode your string before setting it (I haven't tried that, but I supposed this would work). Remember that properties are sent as XML Nodes, of which the value is the Text part of a node.  
    Herve Roggero, Blue Syntax MVP SQL Azure Co-Author: Pro SQL Azure
    • Marked as answer by Eliott Bartley Sunday, February 13, 2011 11:17 PM
    Sunday, February 13, 2011 6:40 PM

All replies

  • If nothing else you could “quote” the data with a surrounding delimiter character.


    David Pallmann GM Application Development, Neudesic Windows Azure MVP
    Friday, February 11, 2011 3:36 PM
  • Interesting... how about trying to use the blank character? ALT+255 instead of space.

    On another note, why is that a problem? 


    Herve Roggero, Blue Syntax MVP SQL Azure Co-Author: Pro SQL Azure
    Saturday, February 12, 2011 4:29 AM
  • Thank you David Pallmann,

    I'll hope there's an alternative solution, so I won't have to analyse all my tables to determine whether they need this fix, but it's good to have this option.

    Kind regards.

    Saturday, February 12, 2011 7:18 PM
  • Thank you Herve Roggero,

    I tried with ALT+255 and that works, it's being saved correctly.

    The reason it's a problem is: as head of security, I insist all data is base-31 encoded into the 0x01-0x20 white-space range, following the "security-ink" principle. It's my own little 0x5f375a86, inspiration of genius, to the cryptography underworld... Ha! I kid ;) Actually, we're storing a user-chosen field-delimiter character for a parser, and some of our darn users want to use white-space. Not really a problem, more a bug in our code traced to an unexpected source--but also wondering now, in what other scenarios can we expect the data to be transparently modified (could it not have thrown an error if it can't store white-space).

    Kind regards.

    Saturday, February 12, 2011 8:20 PM
  • OK - I think I get it.  I would suggest two alternatives; the first one is to transform your characters into a byte array; that way everything will be stored "as-is".  The other one is to URL Encode your string before setting it (I haven't tried that, but I supposed this would work). Remember that properties are sent as XML Nodes, of which the value is the Text part of a node.  
    Herve Roggero, Blue Syntax MVP SQL Azure Co-Author: Pro SQL Azure
    • Marked as answer by Eliott Bartley Sunday, February 13, 2011 11:17 PM
    Sunday, February 13, 2011 6:40 PM
  • Thank you Herve Roggero,

    URL encoding seems to work well, while still being almost human-readable in Azure Storage Explorer; I'll use it.

    Thank you again.

    Kind regards.

    Sunday, February 13, 2011 11:17 PM