Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to make a list that contain lists which contain lists

How to make a list that contain lists which contain lists

Scheduled Pinned Locked Moved C#
xmltutorialquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kmkmahesh
    wrote on last edited by
    #1

    name1
    data1
    category1

    name2
    data2
    category2

    name3
    data3
    category2//same category as above

    ...
    ...
    name20
    data20
    category16

    Now I need to create an List with category1, category2, ... upto what the categories is there on Category Node, need to create a separate Lists of each category to store the item name and every item list will have data of the item so I can retrieve them for my use.

    From above sample xml I need the same category items in a single List

    Items and Category count is fully dynamic. How to do this dynamically?

    OriginalGriffO P L B 4 Replies Last reply
    0
    • K kmkmahesh

      name1
      data1
      category1

      name2
      data2
      category2

      name3
      data3
      category2//same category as above

      ...
      ...
      name20
      data20
      category16

      Now I need to create an List with category1, category2, ... upto what the categories is there on Category Node, need to create a separate Lists of each category to store the item name and every item list will have data of the item so I can retrieve them for my use.

      From above sample xml I need the same category items in a single List

      Items and Category count is fully dynamic. How to do this dynamically?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind. Please, try to explain again, but this time remembering that we have no context in which to work unless you explicitly tell us!

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • K kmkmahesh

        name1
        data1
        category1

        name2
        data2
        category2

        name3
        data3
        category2//same category as above

        ...
        ...
        name20
        data20
        category16

        Now I need to create an List with category1, category2, ... upto what the categories is there on Category Node, need to create a separate Lists of each category to store the item name and every item list will have data of the item so I can retrieve them for my use.

        From above sample xml I need the same category items in a single List

        Items and Category count is fully dynamic. How to do this dynamically?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You've pretty much answered the question yourself. Whatever deserializes this structure will contain a List collection, and Item will be made up of Name, Data and Category. So, you'll have two classes there:

        public class Item
        {
        public string Name;
        public string Data;
        public string Category;
        }

        public class MainList
        {
        public List Items = new List();
        // Do your deserialize and populate Items.
        }

        This space for rent

        1 Reply Last reply
        0
        • K kmkmahesh

          name1
          data1
          category1

          name2
          data2
          category2

          name3
          data3
          category2//same category as above

          ...
          ...
          name20
          data20
          category16

          Now I need to create an List with category1, category2, ... upto what the categories is there on Category Node, need to create a separate Lists of each category to store the item name and every item list will have data of the item so I can retrieve them for my use.

          From above sample xml I need the same category items in a single List

          Items and Category count is fully dynamic. How to do this dynamically?

          L Offline
          L Offline
          LeHuuTien
          wrote on last edited by
          #4

          I have an example. I think my example can help you. I have a class

          public class Action_Info
          {
          public string valueResult { get; set; }
          public string valueError { get; set; }
          public string valueInfo { get; set; }
          public long timeWait { get; set; }
          }

          And a list:

          List<Action_Info> listActions;

          I have a xml file: <Script> <Action> <Result>Success</Result> <ErrorReason>None</ErrorReason> <InfoID></InfoID> <TimeWait>10941</TimeWait> </Action> <Action> <Result></Result> <ErrorReason></ErrorReason> <InfoID>NewRcvXbarRsData</InfoID> <TimeWait>17666</TimeWait> </Action> <Action> <Result></Result> <ErrorReason></ErrorReason> <InfoID>NewRcvXbarRsData</InfoID> <TimeWait>17953</TimeWait> </Action> </Script> Then I read from xml file to listActions:

          XDocument doc = XDocument.Load(filePathXML);
          listActions = doc.Descendants("Action").Select(d =>
          new Action_Info
          {
          valueResult = d.Element("Result").Value,
          valueError = d.Element("ErrorReason").Value,
          valueInfo = d.Element("InfoID").Value,
          timeWait = long.Parse(d.Element("TimeWait").Value)
          }).ToList();

          You can do what you want with your list.

          1 Reply Last reply
          0
          • K kmkmahesh

            name1
            data1
            category1

            name2
            data2
            category2

            name3
            data3
            category2//same category as above

            ...
            ...
            name20
            data20
            category16

            Now I need to create an List with category1, category2, ... upto what the categories is there on Category Node, need to create a separate Lists of each category to store the item name and every item list will have data of the item so I can retrieve them for my use.

            From above sample xml I need the same category items in a single List

            Items and Category count is fully dynamic. How to do this dynamically?

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Assuming: 1. you have deserialized the XML into an instance of a class named 'Main that contains a List<Item> 2. what you want to produce is a Dictionary<string, List<Item>> where each Key in the Dictionary is the Category, and the Value of each Key is a List of those Items with the same Category field value

            // required
            using System.Linq;

            public Dictionary> GetItemsByCategory(List items)
            {
            return items
            .ToLookup(itm => itm.Category)
            .ToDictionary(ky => ky.Key, val => val.ToList());
            }

            If you call the method shown here, passing it the list of Items, what is returned is a Dictionary of the Type mentioned in #2 here.

            «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups