how to get XML attribute using XmlSerializer in c# -
i have class , xml file using values.
xml file
<task> <employee id="123"> <string>/name</string> <string>/company</string> </employee> <manager id="456"> <string>/name</string> <string>/company</string> </manager> </task> code
public class task { public list<string> employee; public list<string> manager; } var taks = (task)new xmlserializer(typeof(task)).deserialize(streamreader); so, in tasks getting list of employee name , compnay values correctly. want id each element. how it?
/name , /company can anything. can put value in there , in employee without creating property it. same goes manager well, can have /email, /website, /lastlogin etc , in manager object without creating property it.
appreciate time , help.
define task class follows:
public class task { public employee employee; public manager manager; } where employee is:
public class employee { [xmlattribute] public string id {get;set;} public string name{get;set;} public string company{get;set;} } and manager is:
public class manager { [xmlattribute] public string id {get;set;} public string name{get;set;} public string company{get;set;} } if you're interested in generic list of properties employee and/or manager, consider having class called property following:
public class property { [xmlattribute] public string value{get;set;} } then, change manager , employee following:
public class employee { [xmlattribute] public string id {get;set;} public list<property> properties {get;set;} } public class manager { [xmlattribute] public string id {get;set;} public list<property> properties {get;set;} } finally, change xml following:
<task> <employee id="123"> <properties> <property value="/name" /> <property value="/company"/> </properties> </employee> <manager id="456"> <properties> <property value="/name" /> <property value="/company"/> </properties> </manager> </task>
Comments
Post a Comment