从xml文件获取标题列表

时间:2009-05-12 17:03:33

标签: c# xml linq

我正在尝试从文件夹调用“bugs”获取xml文件的标题。

我的代码:

    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;
    }

但是我没有找到文件夹“bugs”中所有xml文件的标题。


最大的问题是将eatch文件转换为singel字符串而不是字符串[]。

4 个答案:

答案 0 :(得分:2)

您编写的代码没有任何意义。也许你的意思更像这样:

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;
}

答案 1 :(得分:1)

“from b in bugs”从空列表中选择。你需要在foreach循环开始时从文件初始化bug

答案 2 :(得分:0)

你需要在xmlStorageLocation和“bugs”之间使用反斜杠(Path.DirectorySeparatorChar)吗?

答案 3 :(得分:0)

你的循环中没有使用file - 这是正确的还是你错过了将它推入集合中?

相关问题