PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/CsvReader.Azure/GenericTableReader.cs

https://github.com/tpwalke2/DataTable
C# | 65 lines | 53 code | 8 blank | 4 comment | 6 complexity | 5200ee7686355441029179132cec8caa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Services.Client;
  6. using System.Xml.Linq;
  7. using System.Xml;
  8. using System.Data.Services.Common;
  9. namespace DataAccess
  10. {
  11. [DataServiceKey("PartitionKey", "RowKey")]
  12. internal class GenericEntity
  13. {
  14. public string PartitionKey { get; set; }
  15. public string RowKey { get; set; }
  16. public Dictionary<string, string> properties = new Dictionary<string, string>();
  17. }
  18. internal class GenericTableReader
  19. {
  20. public static XNamespace AtomNamespace = "http://www.w3.org/2005/Atom";
  21. public static XNamespace DataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices";
  22. public static XNamespace MetadataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
  23. // This manually parses the XML that comes back.
  24. // This function uses code from this blog entry:
  25. // http://blogs.msdn.com/b/avkashchauhan/archive/2011/03/28/reading-and-saving-table-storage-entities-without-knowing-the-schema-or-updating-tablestorageentity-schema-at-runtime.aspx
  26. public static void OnReadingEntity(object sender, ReadingWritingEntityEventArgs args)
  27. {
  28. GenericEntity entity = args.Entity as GenericEntity;
  29. if (entity == null)
  30. {
  31. return;
  32. }
  33. // read each property, type and value in the payload
  34. var properties = args.Entity.GetType().GetProperties();
  35. var q = from p in args.Data.Element(AtomNamespace + "content")
  36. .Element(MetadataNamespace + "properties")
  37. .Elements()
  38. where properties.All(pp => pp.Name != p.Name.LocalName)
  39. select new
  40. {
  41. Name = p.Name.LocalName,
  42. IsNull = string.Equals("true", p.Attribute(MetadataNamespace + "null") == null ? null : p.Attribute(MetadataNamespace + "null").Value, StringComparison.OrdinalIgnoreCase),
  43. TypeName = p.Attribute(MetadataNamespace + "type") == null ? null : p.Attribute(MetadataNamespace + "type").Value,
  44. p.Value
  45. };
  46. foreach (var dp in q)
  47. {
  48. string value = dp.Value;
  49. if (!string.IsNullOrWhiteSpace(value))
  50. {
  51. value = string.Empty;
  52. }
  53. entity.properties[dp.Name] = dp.Value;
  54. }
  55. }
  56. }
  57. }