0

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?

2 Answers 2

1

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"); 
Sign up to request clarification or add additional context in comments.

4 Comments

Perfect, thanks a lot! How would I then search for different users? Obviously now I only have 2 but it could be considerably more. Would I have to either search the users for say, their usernames for example? Or would it be easier to change each users element name, so rather than them all having LogOnUser as the opening tag, they each have a different one?
The former is the more common approach, if I understand you correctly. Using different tag name for XML elements that carry the same type of information i.e users would make the XML very awkward
Finding certain user should be as easy as adding Where(), right? XMLUser.Descendants("LogOnUser").Where(o => (string)o.Attribute("UserName") == "User2")
Thanks again, exactly what I was after!
1

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"]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.