c# - Add an XML wrapper element to serialized data -
i have couple of lists have serialized xml file.
i having difficulty figuring out how set xml wrapper element around element list.
currently xml looks this:
<finding> <issue1></issue1> <issue2><issue2 /> <issue3><issue3 /> </finding> <finding> <issue1></issue1> <issue2><issue2 /> <issue3><issue3 /> </finding>
i want wrap these finding
elements xml looks instead:
<findings> <finding> <issue1></issue1> <issue2><issue2 /> <issue3><issue3 /> </finding> <finding> <issue1></issue1> <issue2><issue2 /> <issue3><issue3 /> </finding> <findings/>
here how serializing:
[xmlelement("finding")] public list<findings> findings { {return findings;}} [xmlelement("issue1")] public string getissue1 { { homecoming issue1; } } [xmlelement("issue2")] public string getissue2 { { homecoming issue2; } } [xmlelement("issue3")] public string getissue3 { { homecoming issue3; } }
here calling code:
xmlserializer ser = new xmlserializer(typeof(ruleset)); using (streamwriter author = new streamwriter(filename)) { ser.serialize(writer, findings); }
findings class:
[serializable()] [xmlroot("findings")] public class findings { public string issue1; public string issue2; public string issue3; public string issue4; public findings() { } public findings(string string flag1, string flag2, string flag3, string flag4) { this.issue1 = flag1; this.issue2 = flag2; this.issue3 = flag3; this.issue4 = flag4; } } }
and ruleset class
[serializable()] public class ruleset { private list<findings> findings = new list<findings>(); findings findresults = new findings(); [xmlelement("finding")] public list<findings> findings { {return findings;}} [xmlelement("issue1")] public string getissue1 { { homecoming findresults.issue1; } } [xmlelement("issue2")] public string getissue2 { { homecoming findresults.issue2; } } [xmlelement("issue3")] public string getissue3 { { homecoming findresults.issue3; } } [xmlelement("issue4")] public string getissue4 { { homecoming findresults.issue4; } } public void setfindings(list<findings> findings) { this.findings = findings; } public void addfindings(findings findings) { this.findings.add(findings); } } }
i have been fighting hours. missing?
this should work:
public class findings { [system.xml.serialization.xmlelementattribute("finding")] public list<finding> finding = new list<finding>(); } public class finding { public string issue1; public string issue2; public string issue3; }
note xmlelementattribute
names items of list. in case lower case finding
. also, illustration uses fields issue1
etc., properties fine.
c# xml xml-serialization
No comments:
Post a Comment