tags:

views:

283

answers:

3

In the first scenario, everything is pretty easy, I have the following XML, that I can deserialize to a strong type.

<providers>
   <dprovider>
      <dimensions>
         <width></width>
     </dimensions>
   </dbrovider>

In the 2nd scenario, and this is where I need help... I have something a little more dynamic

<providers>
   <dprovider>
      <dimensions>
         <width></width>
     </dimensions>
   </dbrovider>
     <dprovider>
      <dimensions>
         <height></height>
     </dimensions>
   </dbrovider>

As you can see the dimensions sub structure is differant in both items, so I need to find a way to create the strongly typed class, so that it can handle any change to the dimensions element, it could for example contain a whole nested sub structure, differant for each dbprovider.

Any ideas on how this is done?>

Thanks

A: 

The first thing that jumps to mind is custom deserialization. At that point, your dprovider class can have a List<object> called Dimensions, which has the host of subjobjects you require. The only pain is pulling them back out, because you'll have to do a bunch of "is" logic.

If you're looking to have a "whole nested sub structure", that sub structure should be defined as its own class/struct, then added to the Dimensions list.

JustLoren
if I have list of object, then -> I guess I can hold any object, with any structure in that collection?
JL
Exactly, but I'm not sure if this is filling your "needs to be strongly typed" requirement, since your code will look like:if (dProvider.Dimensions[0] is ObjWithHeight) height = ((ObjWithHeight)dProvider.Dimensions[0]).Height;Having to do that casting every time is a royal pain. If you were to provide more clarification on what you're end goal is (rather than the method you chose), there might be a different method the community could suggest than custom deserialization + Object list + casting.
JustLoren
+1  A: 

I think you might be missing the point of C# to XML Serialization/Deserialization.

You should either define a schema (or use the one you already have) and create a C# class that supports it. See MSDN article on xsd.exe

The other route is to create your C# class (and wrap it in a collection from your markup) and add XML attributes. This will essentially define your XML schema. See here for starting point on the XmlRootAttribute

confusedGeek
You may want to use the DataContractSerializer which can handle collections better then the normal xml serializer. I cannot tell though if you need it.
Stefan Egli
+2  A: 

Try this:

[Serializable]
class Dimension { ... }

[Serializable]
class Height : Dimension { ... }

[Serializable]
class Width : Dimension { ... }

Then, in the class that your (note the plural) element deserializes into, have this property:

[XmlElement( Type = typeof( Height ), ElementName = "height" )]
[XmlElement( Type = typeof( Width ), ElementName = "width" )]
public Dimension[] DimensionArray {
    get { ... }
    set { ... }
}

Here's the starting point of documentation on XML serialization (XmlElement class), so you can figure out the rest of the details.

You should be able to serialize/deserialize your providers object by simply decorating the right properties and classes with the right attributes from the System.Xml.Serialization namespace.

azheglov