【问题标题】:How to save directory list to xml file C#如何将目录列表保存到xml文件C#
【发布时间】:2011-03-27 05:19:25
【问题描述】:

我已经找遍了这个。可能是我在搜索中输入了错误的东西,我不确定。因此,如果您知道一个很好的教程或示例,请分享。我正在努力学习。

我正在开发一个 C# Windows 窗体应用程序。我将信息(在这种情况下为电影)保存在 XML 文件中。我是这样保存xml文件的。

//Now we add new movie.
            XmlElement nodRoot = doc.DocumentElement;
            string allMyChildren = nodRoot.InnerText;
            string capitalized = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(movieEditNameTextbox.Text);
            int indexLookForNewMake = allMyChildren.IndexOf(capitalized);
            if (indexLookForNewMake >= 0)
            {
                MessageBox.Show("Movie is already saved.", "Error");
            }
            else
            {
                XmlElement el = doc.CreateElement("Name");
                el.InnerText = capitalized;
                doc.DocumentElement.AppendChild(el);
                //Check if Year is really a Number.
                if (movieEditYearTextbox.Text.All(Char.IsDigit))
                {
                    //Remove ' cause it gives errors.
                    string capitalizedFixed = capitalized.Replace("'", "");
                    string capitalizedFinalFixed = capitalizedFixed.Replace("\"", "");
                    //Assign Attribute to each New one.
                    el.SetAttribute("Name", capitalizedFinalFixed);
                    el.SetAttribute("Type", movieEditTypeDropdown.Text);
                    el.SetAttribute("Year", movieEditYearTextbox.Text);
                    //Reset all fields, they don't need data now.
                    movieEditNameTextbox.Text = "";
                    movieEditYearTextbox.Text = "";
                    movieEditTypeDropdown.SelectedIndex = -1;
                    removeMovieTextbox.Text = "";

                    doc.Save("movie.xml");

                    label4.Text = "Movie Has been Edited";
                    loadXml();
                }
                else
                {
                    //Error out. Year not a Number
                    MessageBox.Show("Check movie year. Seems it isn't a number.", "Error");
                }
            }

一切正常。现在我要做的是让它可以选择一个目录,它会搜索目录和子目录并获取文件名并将它们保存到 XML 文件中。

我用它来尝试实现这一点。它确实拉动了列表。但它不保存它。它不保存新信息。

我不能使用 LINQ,因为它会由于某种原因与其他代码发生冲突。

DirectoryInfo dirCustom = new DirectoryInfo(@"D:\Video");
        FileInfo[] filCustom;
        filCustom = dirCustom.GetFiles("*",SearchOption.AllDirectories);

        //Open XML File.
        XmlDocument doc = new XmlDocument();
        doc.Load("movie.xml");

        XmlElement el = doc.CreateElement("Name");
        string fulCustoms = filCustom.ToString();

        foreach (FileInfo filFile in filCustom)
        {
            string capitalized = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(filFile.Name);
            string capitalizedFixed = capitalized.Replace("\"", "");
            el.SetAttribute("Name", capitalizedFixed);
            el.SetAttribute("Type", "EDIT TYPE");
            el.SetAttribute("Year", "EDIT YEAR");

            richTextBox1.AppendText(capitalizedFixed + "\r\n");
        }
        doc.Save("movie.xml");

        label4.Text = "Movie Has been Edited";
        loadXml();

现在,richTextBox 确实正确显示了信息,但它没有保存它。 loadXml() 只是我刷新数据网格视图的笨方法。

我完全迷路了,不知道该去哪里。我知道我的编码可能很糟糕,哈哈。我是新手。这是我开发的第一个更复杂的应用程序。

我想不出更多信息可以帮助您理解我的意思。我希望你会。

非常感谢您的帮助。

【问题讨论】:

    标签: c# xml arrays list save


    【解决方案1】:

    不确定您的 LoadXML() 方法究竟做了什么,但我对您的问题的唯一建议是改变您实现此功能的方式。

    创建一个名为 Movie 的对象

    public class Movie 
    {
        public Movie() {}
        public String Title { get; set; }
        blah... blah...
    }
    

    然后创建一个 MovieList

    public class MovieList : List<Movie> { }
    

    然后在 MovieList 中实现以下 2 个方法。

    public static void Serialize(String path, MovieList movieList)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(MovieList));
    
        using (StreamWriter streamWriter = new StreamWriter(path))
        {
           serializer.Serialize(streamWriter, movieList);
        }
    }
    
    public static MovieList Deserialize(String path)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(MovieList));
        using (StreamReader streamReader = new StreamReader(path))
        {
            return (MovieList) serializer.Deserialize(streamReader);
        }
    }
    

    就是这样...您现在已经序列化了您的对象,您可以通过绑定或您选择的任何其他方法检索要填充的数据。

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      相关资源
      最近更新 更多