title: Serializing Name/Value Pairs in a Custom Object via Web Service
body: <p>This is a very complicated question concerning how to serialize data via a web service call, when the data is not-strongly typed.  I'll try to lay it out as best possible.</p>
  
  <p><strong>Sample Storage Object:</strong></p>
  
  <pre><code>[Serializable]
  public class StorageObject {
    public string Name { get; set; }
    public string Birthday { get; set; }
    public List&lt;NameValuePairs&gt; OtherInfo { get; set; }  
  }
  [Serializable]   
  public class NameValuePairs {
    public string Name { get; set; }
    public string Value { get; set; }
  }
  </code></pre>
  
  <p><strong>Sample Use:</strong></p>
  
  <pre><code>[WebMethod]
      public List&lt;StorageObject&gt; GetStorageObjects() {
        List&lt;StorageObject&gt; o = new List&lt;StorageObject&gt;() {
          new StorageObject() { 
            Name = "Matthew",
            Birthday = "Jan 1st, 2008", 
            OtherInfo = new List&lt;NameValuePairs&gt;() {
              new NameValuePairs() { Name = "Hobbies", Value = "Programming" },
              new NameValuePairs() { Name = "Website", Value = "Stackoverflow.com" }
            }
          },
          new StorageObject() { 
            Name = "Joe",
            Birthday = "Jan 10th, 2008",
            OtherInfo = new List&lt;NameValuePairs&gt;() { 
              new NameValuePairs() { Name = "Hobbies", Value = "Programming" },
              new NameValuePairs() { Name = "Website", Value = "Stackoverflow.com" }
            }
          }
        };
  
        return o;
      }
  </code></pre>
  
  <p><strong>Return Value from Web Service:</strong></p>
  
  <pre><code>&lt;StorageObject&gt;
      &lt;Name&gt;Matthew&lt;/Name&gt;
      &lt;Birthday&gt;Jan 1st, 2008&lt;/Birthday&gt;
      &lt;OtherInfo&gt;
          &lt;NameValuePairs&gt;
              &lt;Name&gt;Hobbies&lt;/Name&gt;
              &lt;Value&gt;Programming&lt;/Value&gt;
          &lt;/NameValuePairs&gt;
          &lt;NameValuePairs&gt;
              &lt;Name&gt;Website&lt;/Name&gt;
              &lt;Value&gt;Stackoverflow.com&lt;/Value&gt;
          &lt;/NameValuePairs&gt;
      &lt;/OtherInfo&gt;
  &lt;/StorageObject&gt;
  </code></pre>
  
  <p><strong>What I want:</strong></p>
  
  <pre><code>&lt;OtherInfo&gt;
      &lt;Hobbies&gt;Programming&lt;/Hobbies&gt;
      &lt;Website&gt;Stackoverflow.com&lt;/Website&gt;
  &lt;/OtherInfo&gt;
  </code></pre>
  
  <p><strong>The Reason &amp; Other Stuff:</strong></p>
  
  <p>First, I'm sorry for the length of the post, but I wanted to give reproducible code as well.  </p>
  
  <p>I want it in this format, because I'm consuming the web services from PHP.  I want to easily go:</p>
  
  <p>//  THIS IS IMPORANT</p>
  
  <pre><code>In PHP =&gt; "$Result["StorageObject"]["OtherInfo"]["Hobbies"]".  
  </code></pre>
  
  <p>If it's in the other format, then there would be no way for me to accomplish that, at all.  Additionally, in C# if I am consuming the service, I would also like to be able to do the following:</p>
  
  <p>//  THIS IS IMPORANT</p>
  
  <pre><code>In C# =&gt; var m = ServiceResult[0].OtherInfo["Hobbies"];
  </code></pre>
  
  <p>Unfortunately, I'm not sure how to accomplish this.  I was able to get it this way, by building a custom Dictionary that implemented IXmlSerializer (see <a href="https://stackoverflow.com/questions/67959/c-xml-serialization-gotchas">StackOverflow:  IXmlSerializer Dictionary</a>), however, it blew the WSDL schema out of the water.  It's also much too complicated, and produced horrible results in my WinFormsTester application!</p>
  
  <p>Is there any way to accomplish this ?  What type of objects do I need to create ?  Is there any way to do this /other than by making a strongly typed collection/ ?  Obviously, if I make it strongly typed like this:</p>
  
  <pre><code>public class OtherInfo {
    public string Hobbies { get; set; }
    public string FavoriteWebsite { get; set; }
  }
  </code></pre>
  
  <p>Then it would work perfectly, I would have no WSDL issues, I would be able to easily access it from PHP, and C# (.OtherInfo.Hobbies). </p>
  
  <p>However, I would completely lose the point of NVP's, in that I would have to know in advance what the list is, and it would be unchangeable.. say, from a Database.</p>
  
  <p>Thanks everyone!!  I hope we're able to come up with some sort of solution to this.  Here's are the requirements again:</p>
  
  <ol>
  <li>WSDL schema should not break</li>
  <li>Name value pairs (NVP's) should be serialized into attribute format</li>
  <li>Should be easy to access NVP's in PHP by name ["Hobbies"]</li>
  <li>Should be easy to access in C# (and be compatible with it's Proxy generator)</li>
  <li>Be easily serializable</li>
  <li>Not require me to strongly type the data</li>
  </ol>
  
  <p>Now, I am /completely/ open to input on a better/different way to do this.  I'm storing some relatively "static" information (like Name), and a bunch of pieces of data.  If there's a better way, I'd love to hear it.</p>