Reputation: 201
I have an xml file like 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 I have a class with properties that match the 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
{
get { return _UserName; }
set { _UserName = value; }
}
public string Password
{
get { return _Password; }
set { _Password = value; }
}
public bool LoggedIn
{
get { return _LoggedIn; }
set { _LoggedIn = value; }
}
public Rights UserRights
{
get { return _UserRights; }
set { _UserRights = value; }
}
#endregion
}
I've tried using XDocument to read certain values from the xml file into instances of my LogOnUser class, however I've become a little stuck and after trawling through the documentation on MSDN and not finding what I'm looking for I figured it might be quicker and easier to ask a question on here.
Basically to begin with I've just tried to read the UserName
field from the XML into the UserName
property of a LogOnUser
object. I tried this:
var XMLUser = XElement.Load(LogOnUserXMLFilePath);
foreach(XElement e in XMLUser.DescendantsAndSelf())
{
var user = new Model.LogOnUsers.LogOnUser(e.Name.ToString());
}
however this sets the UserName
to "LogOnUsersInfo" so obviously I need to go deeper into the xml but I don't know how.
Can anyone point me in the right direction?
Upvotes: 0
Views: 62
Reputation: 651
You can do an XPath search. You can get all the user info.
var nodes = doc.SelectNodes("//LogOnUser");
foreach(var node in nodes)
{
var username = node.Attributes["Username"];
}
Upvotes: 1
Reputation: 89335
As a starter, you can do as follow :
foreach(XElement e in XMLUser.Descendants("LogOnUser"))
{
var user = new Model.LogOnUsers.LogOnUser((string)e.Attribute("UserName"));
}
Basically, you can use Descendants()
to go down the XML tree an arbitrary level deep, and use Attribute()
to access XML attribute.
Then rights information can be obtained as follow, for example :
var cancreateproducts = (bool)e.Element("Rights")
.Element("CanCreateProducts")
.Attribute("value");
Upvotes: 1