C# XML Serialization - How to Serialize attribute in Class that inherits List<Object>? -
i need create xml file using c#. using class inherits list represents list of computers , later initialize values serializer doesn't attributes class, descendants. class:
public class computers : list<computer> { [xmlattribute("storagetype")] public int storagetype { get; set; } [xmlattribute("storagename")] public string storagename { get; set; } } public class computer { [xmlattribute("storagetype")] public int storagetype { get; set; } [xmlattribute("storagename")] public string storagename { get; set; } public string ipaddress { get; set; } public string name { get; set; } }
the result should this:
<fpc4:computers storagename="computers" storagetype="1"> <fpc4:computer storagename="{d37291ca-d1a7-4f34-87e4-8d84f1397bea}" storagetype="1"> <fpc4:ipaddress dt:dt="string">127.0.0.1</fpc4:ipaddress> <fpc4:name dt:dt="string">computer1</fpc4:name> </fpc4:computer> <fpc4:computer storagename="{afe5707c-ea71-4442-9ca8-2a6264eaa814}" storagetype="1"> <fpc4:ipaddress dt:dt="string">127.0.0.1</fpc4:ipaddress> <fpc4:name dt:dt="string">computer2</fpc4:name> </fpc4:computer>
but far this:
<fpc4:computers> <fpc4:computer storagetype="1" storagename="{7297fc09-3142-4284-b2e9-d6ea2fb1be78}"> <fpc4:ipaddress>127.0.0.1</fpc4:ipaddress> <fpc4:name>computer1</fpc4:name> </fpc4:computer> <fpc4:computer storagetype="1" storagename="{eab517f6-aca9-4d01-a58b-143f2e3211e7}"> <fpc4:ipaddress>127.0.0.1</fpc4:ipaddress> <fpc4:name>computer2</fpc4:name> </fpc4:computer> </fpc4:computers>
as can see computers node parent node doesn't attributes.
do guys have solution?
xmlserializer
treats lists separate leaf nodes; properties on lists do not exist - collection of contained data. improve approach be:
public class computers { private readonly list<computer> items = new list<computer>(); [xmlelement("computer")] public list<computer> items { { homecoming items; } } [xmlattribute("storagetype")] public int storagetype { get; set; } [xmlattribute("storagename")] public string storagename { get; set; } }
this object has set of computers , has 2 attributes - not list itself. utilize of xmlelementattribute
list flattens nesting desired. note have omitted namespaces convenience.
inheriting list (with aim of adding members) not work well, not xmlserlaizer
, wide range of serializers , binding frameworks.
c# xml xml-serialization xmlserializer
No comments:
Post a Comment