2011-03-27 53 views
0

我已經看了這麼多。這可能是我只是在搜索中輸入錯誤的東西,我不確定。所以,如果你知道一個很好的教程或例子,請分享。我正在嘗試學習。如何將目錄列表保存到xml文件C#

我有一個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()只是我刷新datagridview的方式。

我完全失去了,不知道該去哪裏。我知道我的編碼很可怕,哈哈。我對此很陌生。這是我工作過的第一個更復雜的應用程序。

我再也想不到可以幫助你理解我的意思的信息。我希望你會。

非常感謝您的幫助。

回答

2

不確定你的LoadXML()方法到底是什麼,但是我唯一的建議就是改變你實現這個功能的方式。

創建一個名爲電影對象

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

然後創建一個MovieList

​​

然後實現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); 
    } 
} 

完蛋了......你現在有你的對象序列化,你可以檢索數據通過你選擇的結合或任何其他方法來填充。