c# - Reading from an xml file -


i have xml file this:

<?xml version="1.0" encoding="utf-8" ?> <logonusersinfo>   <generalinformation>     <numberofusers>2</numberofusers>     <lastuser username="user1"/>   </generalinformation>   <logonusercollection>     <logonuser username="user1" password="password">       <rights>         <cancreateproducts value="true"/>         <caneditproducts value="true"/>         <candeleteproducts value="true"/>         <cancreatelogins value="true"/>         <candeletelogins value="true"/>         <canbedeleted value="true"/>         <hasdebugrights value="false"/>       </rights>     </logonuser>       <logonuser username="user2" password="password">       <rights>         <cancreateproducts value="true"/>         <caneditproducts value="true"/>         <candeleteproducts value="true"/>         <cancreatelogins value="true"/>         <candeletelogins value="true"/>         <canbedeleted value="true"/>         <hasdebugrights value="false"/>       </rights>     </logonuser>   </logonusercollection> </logonusersinfo> 

and have class properties match values in said xml file:

public class logonuser {     #region nestedtypes     public class rights     {         public bool cancreateproducts { get; set; }         public bool caneditproducts { get; set; }         public bool candeleteproducts { get; set; }         public bool cancreatelogins { get; set; }         public bool candeletelogins { get; set; }          public bool canbedeleted { get; set; }         public bool hasdebugrights { get; set; }     };     #endregion      #region members     string _username;     string _password;     bool _loggedin;     rights _userrights;      #endregion      #region construction     public logonuser() { }      public logonuser(string username)     {         _username = username;     }      public logonuser(string username, string password, bool cancreateproducts, bool caneditproducts, bool candeleteproducts, bool cancreatelogins, bool candeletelogins)     {         _username = username;         _password = password;          _userrights = new rights();          _userrights.cancreateproducts = cancreateproducts;         _userrights.caneditproducts = caneditproducts;         _userrights.candeleteproducts = candeleteproducts;         _userrights.cancreatelogins = cancreatelogins;         _userrights.candeletelogins = candeletelogins;     }     #endregion      #region properties     public string username     {         { return _username; }         set { _username = value; }     }      public string password     {         { return _password; }         set { _password = value; }     }      public bool loggedin     {         { return _loggedin; }         set { _loggedin = value; }     }      public rights userrights     {         { return _userrights; }         set { _userrights = value; }     }     #endregion } 

i've tried using xdocument read values xml file instances of logonuser class, i've become little stuck , after trawling through documentation on msdn , not finding i'm looking figured might quicker , easier ask question on here.

basically begin i've tried read username field xml username property of logonuser object. tried this:

var xmluser = xelement.load(logonuserxmlfilepath);  foreach(xelement e in xmluser.descendantsandself()) {     var user = new model.logonusers.logonuser(e.name.tostring()); } 

however sets username "logonusersinfo" need go deeper xml don't know how.

can point me in right direction?

as starter, can follow :

foreach(xelement e in xmluser.descendants("logonuser")) {     var user = new model.logonusers.logonuser((string)e.attribute("username")); } 

basically, can use descendants() go down xml tree arbitrary level deep, , use attribute() access xml attribute.

then rights information can obtained follow, example :

var cancreateproducts = (bool)e.element("rights")                                .element("cancreateproducts")                                .attribute("value"); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -