开发者

How to save directory list to xml file C#

开发者 https://www.devze.com 2023-02-20 05:27 出处:网络
I have looked all over for this. It could be me just typing the wrong thing in search I\'m not sure. So, if you know a good tutorial or example of this please share. I\'m trying to learn.

I have looked all over for this. It could be me just typing the wrong thing in search I'm not sure. So, if you know a good tutorial or example of this please share. I'm trying to learn.

I have a C# Windows Form app I'm working on. I have information (movies in this case) saved in an XML file. I saved the xml file like this.

//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");
                }
            }

That all works fine. Now what I'm trying to do is make it where you can choose a directory, and it search the directory and sub directories and get file names and save them into the XML file.

I used this to try to accomplish this. It does pull the list. But it doesn't save it. It don't save the new information.

I can't use LINQ as it cause a confliction for some reason with other code.

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";
        lo开发者_JAVA技巧adXml();

Now, the richTextBox does display the information correctly but it don't save it. The loadXml() is just my noobish way to refresh the datagridview.

I'm completely lost and don't know where to turn to. I know my coding is probarely horrible, lol. I'm new to this. This is my first more complex application I have worked on.

I can't think of anymore information that would help you understand what I mean. I hope you do.

Thank you so much for your help.


Not sure exactly what your LoadXML() method does but my only piece of advise with your issue is to change the way you are implementing this functionality.

Create an object called Movie

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

Then create a MovieList

public class MovieList : List<Movie> { }

Then implement the following 2 methods inside the MovieList.

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

Thats it... You now have your object serialized and you can retrieve the data to populate through binding or whatever other methods you choose.

0

精彩评论

暂无评论...
验证码 换一张
取 消