Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Array/NameValue Collection Serialization


parag.gadkari NO[at]SPAM gmail.com
12/27/2005 8:22:54 AM
Can a name value collection be serialized as attributes to the Xml
element of class containing this name value collection?
For eg.
if a class pControl:

[XmlRoot("Control")]
public class pControl {
.....
NameValueCollection _dynamicProperties;
.....
}

if the _dynamicProperties contains:

Key Value
-------------------------------
name tstControl
readonly true
type button

Can I serialize the class pControl as :
<Root>
....
<control name="tstControl" readonly="true" type="button" />
....
</Root>
parag.gadkari NO[at]SPAM gmail.com
1/5/2006 4:11:54 PM
I developed a workaround for this problem:
[this might be helpful for those who have problems with serializing
NameValueCollection]

For Serializing (MANUAL)
After Serializing the class in an xml file / stream Load the generated
xml using XmlDocument. Then manually added the Attributes to the root
node and write it to the file.

For Deserializing
Add an array of XmlAttribute[] to the class containing the namevalue
collection
when the class is deserialized, those attributes which were not
associated with any property would be added to this array. Write a
function to add element from XmlAttribute array to this name value
collection (filter out any attribute with ":" in it, 'cause there might
be an attribute specifying the type of your object which you might not
want to save in the namevalue collection).

Parag


[quoted text, click to view]
AddThis Social Bookmark Button