How to make a list that contain lists which contain lists
-
name1
data1
category1name2
data2
category2name3
data3
category2//same category as above...
...
name20
data20
category16Now 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?
-
name1
data1
category1name2
data2
category2name3
data3
category2//same category as above...
...
name20
data20
category16Now 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?
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...
-
name1
data1
category1name2
data2
category2name3
data3
category2//same category as above...
...
name20
data20
category16Now 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?
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
-
name1
data1
category1name2
data2
category2name3
data3
category2//same category as above...
...
name20
data20
category16Now 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?
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.
-
name1
data1
category1name2
data2
category2name3
data3
category2//same category as above...
...
name20
data20
category16Now 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?
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.