Add data to List<class>

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am attempting to add data to a list of a class...so basically the program is looping thru a rss feed and I need to add a few things from the feed to a List<DownloadManager> where the DownloadManager is a class I have created....I create a list at the class level with this - List<DownloadManager> listDM = new List<DownloadManager>();

this code works...except it adds the same data over and over ....not sure why....it is in a foreach loop...so my thinkin is everytime it goes thru the loop it should add the NEW data?
see code below

 
       try            {
                WebClient client = new WebClient();

                foreach (string link in links)
                {

                    string url = link;
                    XmlDocument xmlDoc = new XmlDocument();

                    xmlDoc.Load(link);
                    XmlElement root = xmlDoc.DocumentElement;
                    XmlNodeList nodes = root.SelectNodes("channel/item");

                    foreach (XmlNode node in nodes)
                    {
                        if (node != null)
                        {
                            DownloadManager dm = new DownloadManager();
                            
                            dm.ItemTitle = node["title"].InnerText;
                            dm.Links = node["link"].InnerText;
                            dm.Description = node["description"].InnerText;


                           
                            string description = dm.Description;
                            string[] seperator = { " ", "src=" };
                            string Value = description;
                            string[] imagelink = Value.Split(seperator, StringSplitOptions.None);

                            string imagelink_final = imagelink[2].Replace('"', ' ').Trim();
                            ImageLink.Add(imagelink[2].Replace('"', ' ').Trim());

                            listDM.Add(new DownloadManager { ItemTitle = dm.ItemTitle, Description = dm.Description, Links = dm.Links });


thank you for any help

-InkedGFX
 
Last edited:
First of all, why are you creating two DownloadManager objects per iteration?
C#:
try            {
         WebClient client = new WebClient();
 
         foreach (string link in links)
         {
 
             string url = link;
             XmlDocument xmlDoc = new XmlDocument();
 
             xmlDoc.Load(link);
             XmlElement root = xmlDoc.DocumentElement;
             XmlNodeList nodes = root.SelectNodes("channel/item");
 
             foreach (XmlNode node in nodes)
             {
                 if (node != null)
                 {
                     DownloadManager dm = [B][U][COLOR="#FF0000"]new DownloadManager[/COLOR][/U][/B]();
                      
                     dm.ItemTitle = node["title"].InnerText;
                     dm.Links = node["link"].InnerText;
                     dm.Description = node["description"].InnerText;
 
 
                     
                     string description = dm.Description;
                     string[] seperator = { " ", "src=" };
                     string Value = description;
                     string[] imagelink = Value.Split(seperator, StringSplitOptions.None);
 
                     string imagelink_final = imagelink[2].Replace('"', ' ').Trim();
                     ImageLink.Add(imagelink[2].Replace('"', ' ').Trim());
 
                     listDM.Add([B][U][COLOR="#FF0000"]new DownloadManager[/COLOR][/U][/B] { ItemTitle = dm.ItemTitle, Description = dm.Description, Links = dm.Links });
What's the point of the second one? You're simply populating it with the property values of the first one so why not just use the first one?

As for your question though, I can't see anything obvious in your code that would explain your list being populated with anything but what's in the document. Have you debugged? That should have been the first thing you did. Put a breakpoint at the top of the code and then step through it line by line. You can then see exactly what is happening at every step, viewing data using the Autos, Locals and Watch windows and more. If there's an issue with the code then it should be quite obvious because as soon as reality differs from expectation, you've found it.
 
C#:
public class EmailData
{
    public EmailData(string firstName, string lastName, string location)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Location = location;
    }
    public string FirstName{ set; get; }
    public string LastName { set; get; }
    public string Location{ set; get; }
}
SOURCE:c# - Storing data into list with class - Stack Overflow

That information isn't really of any use. The OP obviously already knows how to create the object and add it to the list. It's making sure that the items in the list contain the correct data that's the issue and your post does nothing to address that. We're always happy for people to help but please make sure that the help you provide does address an actual issue or else it only serves to confuse the matter.
 
That information isn't really of any use. The OP obviously already knows how to create the object and add it to the list. It's making sure that the items in the list contain the correct data that's the issue and your post does nothing to address that. We're always happy for people to help but please make sure that the help you provide does address an actual issue or else it only serves to confuse the matter.
Hi, thank you for your reminder, I WILL DO IT BETTER later.
 
I add two because , I have two list's and to be honest I don't know how to work with the second one.....I would like to use only one .....the first list (dm) is the one I can work with ...the second (listDM) is the list I am attempting to add data to the class list, if I explained that correctly?

the second one has the same data ...I will step thru it later tonight to see where the issue is....

Thank You

-InkedGFX
 
I add two because , I have two list's and to be honest I don't know how to work with the second one.....I would like to use only one .....the first list (dm) is the one I can work with ...the second (listDM) is the list I am attempting to add data to the class list, if I explained that correctly?

the second one has the same data ...I will step thru it later tonight to see where the issue is....

Thank You

-InkedGFX
From the code you're showing, you only have one List and you're only adding on DownloadManager objetc to it. You're not really making much sense I'm afraid.
 
I am trying to add the data from the new node each time the foreach loop fires....the data should be added to the listDM which is a List<DownloadManager> where DownloadManager is a custom class I created.....for some reason the same data gets added over and over until the code reaches the end of the foreach loop...

on a side note...I am also trying to get the data from the <enclosure url> tag in the rss feed...but nothing seems to work.......

some help would be greatly appreciated ....

-INkedGFX
 
I am trying to add the data from the new node each time the foreach loop fires....the data should be added to the listDM which is a List<DownloadManager> where DownloadManager is a custom class I created.....for some reason the same data gets added over and over until the code reaches the end of the foreach loop...

Yeah, and that's one List. I still don't see why you need to create two DownloadManager objects per iteration. Regardless, I said in post #2 that you should debug your code. Have you done so? If not, why not? If so, what did you see?
 
I debugged and the result of the the debug don't match up to what is actually going on......

so I stepped thru the debug and the list indeed gets different data added to the list.....here is the confusing part, the data that is displayed in the datagridview is the same on every line.

so since I couldnt't get the data grid to work , I changed to a listview......is there away to display the data from the List listDM?

thank you for your Help

-InkedGFX
 
I debugged and the result of the the debug don't match up to what is actually going on......

so I stepped thru the debug and the list indeed gets different data added to the list.....here is the confusing part, the data that is displayed in the datagridview is the same on every line.

so since I couldnt't get the data grid to work , I changed to a listview......is there away to display the data from the List listDM?

thank you for your Help

-InkedGFX

What DataGridView? This the first mention of a DataGridView in this whole thread. If the data in the List is correct but the data in the DataGridView is not then obviously the issue is how you populate the DataGridView from the List. As we've never seen that, nor have you even ever mentioned it, we can't possibly know what's wrong with it. If the data in the List is correct then, to display that correct data correctly in a DataGridView, you simply assign the List to the DataSource property of the DataGridView.
 
What DataGridView? This the first mention of a DataGridView in this whole thread. If the data in the List is correct but the data in the DataGridView is not then obviously the issue is how you populate the DataGridView from the List. As we've never seen that, nor have you even ever mentioned it, we can't possibly know what's wrong with it. If the data in the List is correct then, to display that correct data correctly in a DataGridView, you simply assign the List to the DataSource property of the DataGridView.


I removed the datagridview ..and replaced it with a listview

-InkedGFX
 
I removed the datagridview ..and replaced it with a listview

-InkedGFX

Well, you shouldn't have. What you should have done was told us about the DataGridView sooner and we could help you use it properly, because you obviously weren't.
 
Back
Top Bottom