code_junkie

Serializing Name/Value Pairs in a Custom Object via Web Service

Nov 12th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. [Serializable]
  2. public class StorageObject {
  3. public string Name { get; set; }
  4. public string Birthday { get; set; }
  5. public List<NameValuePairs> OtherInfo { get; set; }
  6. }
  7. [Serializable]
  8. public class NameValuePairs {
  9. public string Name { get; set; }
  10. public string Value { get; set; }
  11. }
  12.  
  13. [WebMethod]
  14. public List<StorageObject> GetStorageObjects() {
  15. List<StorageObject> o = new List<StorageObject>() {
  16. new StorageObject() {
  17. Name = "Matthew",
  18. Birthday = "Jan 1st, 2008",
  19. OtherInfo = new List<NameValuePairs>() {
  20. new NameValuePairs() { Name = "Hobbies", Value = "Programming" },
  21. new NameValuePairs() { Name = "Website", Value = "Stackoverflow.com" }
  22. }
  23. },
  24. new StorageObject() {
  25. Name = "Joe",
  26. Birthday = "Jan 10th, 2008",
  27. OtherInfo = new List<NameValuePairs>() {
  28. new NameValuePairs() { Name = "Hobbies", Value = "Programming" },
  29. new NameValuePairs() { Name = "Website", Value = "Stackoverflow.com" }
  30. }
  31. }
  32. };
  33.  
  34. return o;
  35. }
  36.  
  37. <StorageObject>
  38. <Name>Matthew</Name>
  39. <Birthday>Jan 1st, 2008</Birthday>
  40. <OtherInfo>
  41. <NameValuePairs>
  42. <Name>Hobbies</Name>
  43. <Value>Programming</Value>
  44. </NameValuePairs>
  45. <NameValuePairs>
  46. <Name>Website</Name>
  47. <Value>Stackoverflow.com</Value>
  48. </NameValuePairs>
  49. </OtherInfo>
  50. </StorageObject>
  51.  
  52. <OtherInfo>
  53. <Hobbies>Programming</Hobbies>
  54. <Website>Stackoverflow.com</Website>
  55. </OtherInfo>
  56.  
  57. In PHP => "$Result["StorageObject"]["OtherInfo"]["Hobbies"]".
  58.  
  59. In C# => var m = ServiceResult[0].OtherInfo["Hobbies"];
  60.  
  61. public class OtherInfo {
  62. public string Hobbies { get; set; }
  63. public string FavoriteWebsite { get; set; }
  64. }
  65.  
  66. public class StorageObject {
  67. public string Name { get; set; }
  68. public string Birthday { get; set; }
  69. public List<string> OtherInfo { get; set; }
  70. }
  71.  
  72. OtherInfo = {"Hobbies|Programming", "Website|Stackoverflow.com"}
  73.  
  74. <StorageObject>
  75. <Name>Matthew</Name>
  76. <Birthday>Jan 1st, 2008</Birthday>
  77. <OtherInfo>
  78. <string>Hobbies|Programming</string>
  79. <string>Website|Stackoverflow.com</string>
  80. </OtherInfo>
  81. </StorageObject>
  82.  
  83. public class StorageObject {
  84. public string Name { get; set; }
  85. public string Birthday { get; set; }
  86. public XElement OtherInfo { get; set; } // XmlElement for dot net 2
  87. }
  88.  
  89. XElement OtherInfo = new XElement("OtherInfo");
  90. OtherInfo.Add( ..Hobbies xelement & text value..);
  91. OtherInfo.Add( ..WebSite xelement & text value..);
  92.  
  93. <OtherInfo>
  94. <Hobbies>Programming</Hobbies>
  95. <Website>Stackoverflow.com</Website>
  96. </OtherInfo>
  97.  
  98. XElement OtherInfo = new XElement("OtherInfo");
  99. OtherInfo.Add( ..nvp xattribute Hobbies & value..);
  100. OtherInfo.Add( ..nvp xattribute WebSite & value..);
  101.  
  102. <OtherInfo>
  103. <nvp n="Hobbies" v="Programming" />
  104. <nvp n="Website" v="Stackoverflow.com" />
  105. </OtherInfo>
  106.  
  107. public class StorageObject {
  108. public string Name { get; set; }
  109. public string Birthday { get; set; }
  110. [XmlAnyElement("Info")] // this prevents double-nodes in the XML
  111. public XElement OtherInfo { get; set; }
  112. }
  113.  
  114. StorageObject o = new StorageObject();
  115. o.OtherInfo.Add(new XElement("Hobbies","Programming");
  116. o.OtherInfo.Add(new XElement("Website","Stackoverflow.com");
  117.  
  118. <Info>
  119. <Hobbies>Programming</Hobbies>
  120. <Website>Stackoverflow.com</Website>
  121. </Info>
  122.  
  123. [Serializable]
  124. public class StorageObject {
  125. public string Name { get; set; }
  126. public string Birthday { get; set; }
  127.  
  128. [Nonserializable]
  129. public Dictionary<string,List<string>> OtherInfo { get; set; }
  130. }
  131.  
  132. [WebMethod]
  133. public List<StorageObject> GetStorageObjects() {
  134. // returns list of storage objects from persistent storage or cache
  135. }
  136.  
  137. [WebMethod]
  138. public List<string> GetStorageObjectAttributes( string name )
  139. {
  140. // find storage object, sObj
  141. return sObj.Keys.ToList();
  142. }
  143.  
  144. [WebMethod]
  145. public List<string> GetStorageObjectAtributeValues( sting name, string attribute )
  146. {
  147. // find storage object, sObj
  148. return sObj[attribute];
  149. }
  150.  
  151. List<string> hobbies = storageObject.OtherInfo["Hobbies"];
Add Comment
Please, Sign In to add comment