Avijit
Avijit

Reputation: 1229

linq to generic list C#

I am getting below XML from database:

<Users>
        <User>
            <USRID>1234</USRID>
            <USERNAME>ABCD</USERNAME>
            <ROLES>
                <ROLE>
                    <ROLEID>1</ROLEID>
                    <ROLENAME>GlobalAdministrator</ROLENAME>
                    <ISDEFAULTROLE>1</ISDEFAULTROLE>
                </ROLE>
                <ROLE>
                    <ROLEID>2</ROLEID>
                    <ROLENAME>Administrator</ROLENAME>
                    <ISDEFAULTROLE>0</ISDEFAULTROLE>
                </ROLE>
            </ROLES>
        </User>
        <User>
            <USRID>2312</USRID>
            <USERNAME>XUX</USERNAME>
            <ROLES>
                <ROLE>
                    <ROLEID>3</ROLEID>
                    <ROLENAME>AccountManager</ROLENAME>
                    <ISDEFAULTROLE>1</ISDEFAULTROLE>
                </ROLE>
                <ROLE>
                    <ROLEID>5</ROLEID>
                    <ROLENAME>Approver</ROLENAME>
                    <ISDEFAULTROLE>0</ISDEFAULTROLE>
                </ROLE>
            </ROLES>
        </User>
    </Users>

This is my class structure to populate that XML:

public class Role
    {
        public string RoleId
        {
            get;
            set;
        }
        public string RoleName
        {
            get;
            set;
        }
        public bool IsDefaultRole
        {
            get;
            set;
        }
    }
public class User
    {
        public User()
        {
            UserRoles = new List<Role>();
        }
        public string UserId
        {
            get;
            set;
        }
        public string UserName
        {
            get;
            set;
        }
        public List<Role> UserRoles
        {
            get;
            set;
        }
    }

I would like to use linq to populate "User" class. How?

XDocument doc = XDocument.Parse(userXML);
                lstUser = (from dem in doc
                                       .Descendants("Users")
                                       .Descendants("User")
                           select new User
                           {
                              UserId = dem.Element("USRID").Value,
                              UserName = dem.Element("USERNAME").Value,
                              ..............................   
                           }).ToList();

Please help me to complete the above code. What I have to write in ....... section to get the role?

Upvotes: 0

Views: 138

Answers (3)

Rahul Singh
Rahul Singh

Reputation: 21795

Easy:-

List<User> users = xdoc.Root.Descendants("User")
                       .Select(x => new User
                                {
                                   UserId = x.Element("USRID").Value,
                                   UserName = x.Element("USERNAME").Value,
                                   UserRoles = x.Descendants("ROLE")
                                                .Select(z => new Role
                                                         {
                                                            RoleId = z.Element("ROLEID").Value,
                                                            RoleName = z.Element("ROLENAME").Value,
                                                            IsDefaultRole = z.Element("ISDEFAULTROLE").Value == "1" ? true : false
                                                   }).ToList()
                               }).ToList();

Upvotes: 1

Kutyel
Kutyel

Reputation: 9084

Try this...

var lstUser = doc.Descendants("Users").Descendants("User")
    .Select(dem => new User
    {
        UserId = dem.Element("USRID").Value,
        UserName = dem.Element("USERNAME").Value,
        UserRoles = dem.Descendants("ROLES").Descendants("ROLE")
            .Select(x => new Role
            {
                RoleId = x.Element("ROLEID").Value,
                RoleName = x.Element("ROLENAME").Value,
                IsDefaultRole = x.Element("ISDEFAULTROLE").Value == "1"
            })
           .ToList()
    }).ToList();

Upvotes: 1

SOReader
SOReader

Reputation: 6007

As already mentioned - use xml serializer. Its implementation isn't perfect but for most cases it is ok. It is also possible to use DataMember attributes and DataContractSerializer and deserializer to achieve the same goal.

Upvotes: 1

Related Questions