tags:

views:

102

answers:

6

Hello, I am trying to get titles of xml files from a folder call "bugs".

My code:

    public virtual List<IBug> FillBugs()
    {
        string folder = xmlStorageLocation + "bugs" + Path.DirectorySeparatorChar;

        List<IBug> bugs = new List<IBug>();

        foreach (string file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly))
        {
            var q = from b in bugs
                    select new IBug
                    {
                        Title = b.Title,
                        Id = b.Id,
                    };

            return q.ToList();
        }

        return bugs;
    }

But I'm not geting out the titles from all the xml files in the folder "bugs".

A: 

Do you need a backslash (Path.DirectorySeparatorChar) between xmlStorageLocation and "bugs"?

jeffamaphone
No i don't because xmlStorageLOcation include a Path.DirectorySeparatorChar
Frozzare
+1  A: 

"from b in bugs" selects from an empty list. you need to initialize bugs from the file at the start of your foreach loop

Jimmy
+2  A: 

Your code as written doesn't make any sense. Perhaps you meant something more like this:

public virtual List<IBug> FillBugs()
{
    // is this actually correct or did you mix up the concatenation order?
    // either way, I suggest Path.Combine() instead

    string folder = xmlStorageLocation + "bugs" + Path.DirectorySeparatorChar;

    List<IBug> bugs = new List<IBug>();

    foreach (string file in Directory.GetFiles(folder, "*.xml",
        SearchOption.TopDirectoryOnly))
    {
        // i guess IBug is not actually an interface even though it starts 
        // with "I" since you made one in your code

        bugs.Add(new IBug {
            Title = file, Id = 0 /* don't know where you get an ID */ });
    }

    return bugs;
}
mquander
The title and id are in eatch xml file, and i wan't to get out the title and id from eatch xml file to a list
Frozzare
Then within your "foreach" you need to open the file and parse out the information you need, and then add it to your collection as shown.
GalacticCowboy
A: 

You don't use file in your loop anywhere - Is that correct or did you miss to push it into the collection?

Dario
i miss to push it into the collection,
Frozzare
A: 

I did some change i my code, and i think it should work better now, but i still have a problem. Where fileName is a xml file

public virtual IList<IBug> FillBugs()
        {

            string[] fileEntries = Directory.GetFiles(xmlStorageLocation + "bugs" + Path.DirectorySeparatorChar);
            foreach (string fileName in fileEntries)
            {
                XDocument XmlBug = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath(fileName));

                var q = from b in XmlBug.Descendants("bug")
                        select new IBug
                        {
                            Title = b.Element("title").Value,
                            Id = b.Element("id").Value,
                        };

                return q.ToList();
            }

            return /// what should i return here?


        }
Frozzare
In that location, you should return either null or an empty list. However, please note that your code will only ever match the first file it finds, because you return within the foreach - so it only ever executes once, at the most. If you want to return a list of bugs from all files in the folder, you need to declare your list outside of the foreach, push bug instances into it within the loop and then return the list at the end - whether anything was pushed into it or not.
GalacticCowboy
i can't figure out a way to get the list to run that for all files in the folder..
Frozzare
Because you *RETURN* in your loop. You explicitly tell the application to do precisely what you don't want to do. As mquander pointed out above, you need to use your loop to add values to the list you want to return, and then return the list at the end of the method.
GalacticCowboy
A: 

the biggest problem is to get eatch files to singel string and not string[].

Frozzare