Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Read nested list

$
0
0

Hi,

Below the sample code to play with

<?xml version="1.0" encoding="utf-8"?><UsersList><User><UserName>Bob</UserName><UserAge>34</UserAge><FamilyList><Family><FamilyName>Family1</FamilyName><members>5</members><type>2</type></Family><Family><FamilyName>Family2</FamilyName><members>10</members><type>3</type></Family></FamilyList></User><User><UserName>Rob</UserName><UserAge>35</UserAge><FamilyList><Family><FamilyName>Family1</FamilyName><members>5</members><type>2</type></Family><Family><FamilyName>Family2</FamilyName><members>10</members><type>3</type></Family></FamilyList></User></userList>

cs:

 

public partial class User
    {
        public string UserName{ get; set; }
        public string UserAge{ get; set; }

        public List<Family> FamilyList{ get; set; }
    }

public partial class Family
    {
        public string FamilyName{ get; set; }
        public string members{ get; set; }

        public string type{ get; set; }
    }

sample c# code i triedand not working 

 XElement xelement = XElement.Load(@"D:\users.xml");
            IEnumerable<User> UserList = (from element in xelement.Descendants("User")
                                              select new user
                                              {
                                                  UserName= element.Element("UserName").Value,
                                                  UserAge= element.Element("UserAge").Value,

                                                  FamilyList= (from fm in element.Elements("FamilyList")
                                                                     select new FamilyList
                                                                     {
                                                                         FamilyName=fm.Element("FamilyName"),
                                                                         members=fm.Element("members"),
                                                                         type=ing.Element("type")
                                                                     }).ToList()
                                              }).ToList();

I need to read this xml using linq to xml concept. i have tried ling to xml ways and getting null values when getting the family. basically struggling to get nested list

any sample code will be much helpful



loving dotnet



Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>