从xml文件获取标题列表

时间:2021-07-28 22:47:26

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

我试图从文件夹调用“错误”获取xml文件的标题。

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".

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


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

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

4 个解决方案

#1


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

#2


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

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

#3


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

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

#4


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

你不在任何地方使用你的循环文件 - 这是正确的还是你错过了把它推入集合?

#1


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

#2


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

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

#3


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

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

#4


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

你不在任何地方使用你的循环文件 - 这是正确的还是你错过了把它推入集合?