Deserializing XML into C# class member without nesting tags



I have some xml that looks likes this:



<data>
<typeadatas>
<typea>
<name>Some data of type a</name>
<!-- Other tags -->
</typea>
<!-- And more type a data -->
</typeadatas>
<arrays>
<typea>
<name>Array of data using a type a</name>
<!-- Other tags from above -->
<dimension1>
<!-- tags informing about the dimension -->
</dimension1>
</typea>
</arrays>
</data>


And I need to get this into a series of C# classes to be able to do further processing. Currently I have a series of classes that look like:



class Data : IXmlSerializable {
/* Custom implementation of ReadXml to handle various complications in the xml not shown here */
List<TypeA> TypeAData
List<ArrayType<TypeA>> TypeADataInArrays
}

class BaseType {
public string Name;
/* Other properties */
}

class TypeA : BaseType {
}

class Dimension {
/* Dimension properties */
}

class ArrayType<T> where T : BaseType {
T BaseData
Dimension Dimension1;
}


There exists further data types that are stored, but share (for now) the same set of data as typea does. Thus BaseType is used to store all the common elements. I don't want to rely on this always being true though.


For the array information, they store data about a separate piece of data (So the typea tags are independent in the example given), but I still want to reuse the common data inside a common class for arrays for dealing with dimensions and the like. However, in the given classes, the BaseData member never receives data from the XML deserialization process as C# expects it to be inside another tag. However, I don't want to have another tag there, as it just makes the XML harder to understand.


Thus, besides making a custom ReadXml function for ArrayType, is there a way to have C# automatically the tags from within the typea array into BaseData, without having a tag?


No comments:

Post a Comment