how to read xml attribute using C# classes using deserialization -
below xml file :
<visibilities> <visibilities appname="test2"> <visibility displayname="full name"> <fullname> <firstname propertyname="fullname.firstname" displayname="first name" listorder="3"></firstname> <middlename propertyname="fullname.middlename" displayname="middle name" listorder="2"></middlename> <lastname propertyname="fullname.lastname" displayname="last name" listorder="1"></lastname> </fullname> </visibility> </visibilities> </visibilities>
below classes
#region fullname [serializable] public class fullname { private string strfirstname; [xmlelement("firstname")] public string firstname { { return strfirstname; } set { strfirstname = value; } } private string strmiddlename; [xmlelement("middlename")] public string middlename { { return strmiddlename; } set { strmiddlename = value; } } private string strlastname; [xmlelement("lastname")] public string lastname { { return strlastname; } set { strlastname = value; } } [xmlelement("listorder")] public int listorder { get; set; } } #endregion #region visibility [serializable] public class visibility { public fullname fullname { get; set; } [xmlattribute("displayname")] public string displayname { get; set; } } #endregion #region visibilities [serializable] public class visibilities { [xmlattribute("appname")] public string appname { get; set; } [xmlelement("visibility")] public list<visibility> visibility { get; set; } } #endregion
below lines of codes how reading :
foreach (visibility vis in l.visibility.tolist()) { console.writeline(vis.displayname); console.writeline("{0}{1}", vis.fullname.firstname, reflect.getpropvalue<string>(cust, vis.fullname.firstname)); }
what modifications have can read attribute values i.e propertyname,displayname,listorder first,middle,last name using classes or readind code part?
i think going need class hold 3 properties, , mark properties xmlattribute
attribute
[serializable] public class name { [xmlattribute] public string propertyname { get; set; } [xmlattribute] public string displayname { get; set; } [xmlattribute] public int listorder { get; set; } } [serializable] public class fullname { private name strfirstname; [xmlelement("firstname")] public name firstname { { return strfirstname; } set { strfirstname = value; } } private name strmiddlename; [xmlelement("middlename")] public name middlename { { return strmiddlename; } set { strmiddlename = value; } } private name strlastname; [xmlelement("lastname")] public name lastname { { return strlastname; } set { strlastname = value; } } [xmlelement("listorder")] public int listorder { get; set; } } [serializable] public class visibility { public fullname fullname { get; set; } [xmlattribute("displayname")] public string displayname { get; set; } } [serializable] public class visibilities { [xmlattribute("appname")] public string appname { get; set; } [xmlelement("visibility")] public list<visibility> visibility { get; set; } }
serialized:
<visibilities> <visibilities appname="test2"> <visibility displayname="full name"> <fullname> <firstname propertyname="fullname.firstname" displayname="first name" listorder="3" /> <middlename propertyname="fullname.middlename" displayname="middle name" listorder="3" /> <lastname propertyname="fullname.lastname" displayname="last name" listorder="3" /> <listorder>0</listorder> </fullname> </visibility> </visibilities>
Comments
Post a Comment