A: 

I'm not sure this would solve your problem (it would in C#, but maybe not in PHP), but try using Dictionary<string,List<string>> OtherInfo instead of List<NameValuePairs>. Then "Hobbies" and "Websites" would be your keys and the values would be the list of hobbies or web sites. I'm not sure how it would serialize, though.

You would be able to reference the lists of hobbies as:

List<string> hobbies = storageObject.OtherInfo["Hobbies"];

[EDIT] See here for a generic XML serializable dictionary. This derived class is the one you would need to use instead of generic Dictionary.

tvanfosson
You can't serialize a Dicationry via XML :( Thank you though!
Matthew M.
Dictionary<TKey,TValue> is serializable if you derive from it and mark the derived class as serializable. http://cs.rthand.com/blogs/blog_with_righthand/archive/2005/08/06/How-to-deserialize-a-Dictionary_3C00_TKey_2C00_-TValue_3E00_-descendant-using-binary-formatter.aspx
tvanfosson
However you serialize dictionary in WCF, the datacontractSerializer
codemeit
The referenced code could be customized to represent the element in any format you want so that you can control how it looks to PHP. I'm not familiar with WCF, but I'm taking a class on it in a couple of weeks.
tvanfosson
A: 

Have a look into the System.Xml.Serialization.XmlSerializerAssemblyAttribute attribute. This lets you specify a custom class-level serializer. You'll be able to spit out whatever XML you like.

A quick way to get up to speed on these is to use sgen.exe to generate one and have a peek at it with Reflector.

-Oisin

x0n
+3  A: 

This is like dynamic properties for a object. C# is not quite a dynamic language unlike javascript or maybe PHP can parse the object properties on the fly. The following two methods are what I can think of. The second one might fit into your requirements.

The KISS Way

The Keep It Simple Stupid way

public class StorageObject {
  public string Name { get; set; }
  public string Birthday { get; set; }
  public List<string> OtherInfo { get; set; }  
}

You can have name value pairs which is separated by '|'

OtherInfo = {"Hobbies|Programming", "Website|Stackoverflow.com"}

Serialized forms

<StorageObject>
    <Name>Matthew</Name>
    <Birthday>Jan 1st, 2008</Birthday>
    <OtherInfo>
        <string>Hobbies|Programming</string>
        <string>Website|Stackoverflow.com</string>
    </OtherInfo>
</StorageObject>

The Dynmaic Way in C#

Make the name value pair part become an XML element so that you can build it dynamically.

public class StorageObject {
  public string Name { get; set; }
  public string Birthday { get; set; }
  public XElement OtherInfo { get; set; } // XmlElement for dot net 2
}

You can easily build up OtherInfo object as element centric e.g.

XElement OtherInfo = new XElement("OtherInfo");
OtherInfo.Add( ..Hobbies xelement & text value..);
OtherInfo.Add( ..WebSite xelement & text value..);

The serialized form will be

<OtherInfo>
    <Hobbies>Programming</Hobbies>
    <Website>Stackoverflow.com</Website>
</OtherInfo>

or build it as attribute centric

XElement OtherInfo = new XElement("OtherInfo");
OtherInfo.Add( ..nvp xattribute Hobbies & value..);
OtherInfo.Add( ..nvp xattribute WebSite & value..);

<OtherInfo>
    <nvp n="Hobbies" v="Programming" />
    <nvp n="Website" v="Stackoverflow.com" />
</OtherInfo>

For any dynamic language, it can access to the properties directly. For the rest, they can access the value by read the XML. Reading XML is well supported by most of framework.

codemeit
Thank you!! Both are great ideas, I think I can work with either of those solutions to accomplish my goals. Nice job on the XElement, I would not have thought about that (lack of knowing about that. :P).
Matthew M.
It'd be more interesting once you start dealing with JSON for javascript.
codemeit
+1  A: 

As a completely different take on this, why not think about doing it completely differently. Have one web service method to return the serialized storage object, minus the OtherInfo and another method to return the list of properties (keys) for OtherInfo, and a third to return the list of values for any key. Granted, it will take more round trips to the web service if you want all of the data, but the solution will be much simpler and more flexible.

[Serializable]
public class StorageObject {
  public string Name { get; set; }
  public string Birthday { get; set; }

  [Nonserializable]
  public Dictionary<string,List<string>> OtherInfo { get; set; }  
}

[WebMethod]
public List<StorageObject> GetStorageObjects() {
    // returns list of storage objects from persistent storage or cache
}

[WebMethod]
public List<string> GetStorageObjectAttributes( string name )
{
    // find storage object, sObj
    return sObj.Keys.ToList();
}

[WebMethod]
public List<string> GetStorageObjectAtributeValues( sting name, string attribute )
{
    // find storage object, sObj
    return sObj[attribute];
}
tvanfosson
That's an interesting idea!! I'm not sure it's a good solution for what I'm working on right now, but I can see that it's another way of thinking and it is definitely a solution. Thank you /tvanfosson/.
Matthew M.
+3  A: 

This is what I've settled on.

Class Structure:

public class StorageObject {
  public string Name { get; set; }
  public string Birthday { get; set; }
  [XmlAnyElement("Info")]  // this prevents double-nodes in the XML
  public XElement OtherInfo { get; set; }
}

Usage:

StorageObject o = new StorageObject();
o.OtherInfo.Add(new XElement("Hobbies","Programming");
o.OtherInfo.Add(new XElement("Website","Stackoverflow.com");

Output:

<Info>
  <Hobbies>Programming</Hobbies>
  <Website>Stackoverflow.com</Website>
</Info>

I would like to thank everyone for their assistance, I really appreciate the help and ideas.

Matthew M.