[英]C# - Can't figure out how to read custom objects to file, add new custom objects, and write new set to overwritten file
[英]cannot figure out why this does not write into a new xml file anymore
最近,我對按鈕“保存”事件做了一些更改,它只是將內容寫入xml文件或創建了一個新文件。 我似乎無法弄清楚這里的問題是什么...我的savepath字符串對我來說看起來還可以。 請幫助我找出我的代碼有什么問題。 非常感謝。
這是我的代碼:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
string savepath;
SaveFileDialog DialogSave = new SaveFileDialog();
// Default file extension
DialogSave.DefaultExt = "txt";
// Available file extensions
DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
// Adds a extension if the user does not
DialogSave.AddExtension = true;
// Restores the selected directory, next time
DialogSave.RestoreDirectory = true;
// Dialog title
DialogSave.Title = "Where do you want to save the file?";
// Startup directory
DialogSave.InitialDirectory = @"C:/";
if (DialogSave.ShowDialog().Equals(true))
{
savepath = DialogSave.FileName;
//DialogSave.Dispose();
DialogSave = null;
FormSaving formsaving = new FormSaving();
formsaving.Builderemail = comboBox1.SelectedIndex;
formsaving.Manageremail = comboBox2.SelectedIndex;
if (MajorversionresultLabel != null && MajorversionresultLabel.Content != null && MajorversionLabel.Content.ToString() != string.Empty)
formsaving.Majorversion = MajorversionresultLabel.Content.ToString();
if (MinorversionresultLabel != null && MinorversionresultLabel.Content != null && MinorversionLabel.Content.ToString() != string.Empty)
formsaving.Minorversion = MinorversionresultLabel.Content.ToString();
if (ProjectnumberresultLabel != null && ProjectnumberresultLabel.Content != null && ProjectnumberLabel.Content.ToString() != string.Empty)
formsaving.Projectnumber = ProjectnumberresultLabel.Content.ToString();
if (BuildnumberresultLabel != null && BuildnumberresultLabel.Content != null && BuildnumberLabel.Content.ToString() != string.Empty)
formsaving.Buildnumber = BuildnumberresultLabel.Content.ToString();
if (PreviousbuildversionresultLabel != null && PreviousbuildversionresultLabel.Content != null && PreviousbuildversionresultLabel.Content.ToString() != string.Empty)
formsaving.Previousbuildversion = PreviousbuildversionresultLabel.Content.ToString();
formsaving.Startzbuildfrom = StartzbuildfromcomboBox.SelectedIndex;
formsaving.Fullorsingle = FullorsinglecomboBox.SelectedIndex;
formsaving.Builddrive = BuilddrivecomboBox.SelectedIndex;
if (TruecmtipresultTextBlock != null && TruecmtipresultTextBlock.Text != null && TruecmtipresultTextBlock.Text != string.Empty)
formsaving.Truecmtip = TruecmtipresultTextBlock.Text;
formsaving.Truecmcomments = TruecmcommentsresultTextBlock.Text;
...
using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
serializer.Serialize(savestream, formsaving);
}
}
DialogSave.Dispose();
}
您正在使用哪個SaveFileDialog類? 一個來自Microsoft.Win32,一個來自System.Windows.Forms?
第一個返回bool ?,因此您的代碼應該可以(將DialogSave設置為null,然后將其放置在if塊之外)。
如果您使用的是第二個,它將返回一個DialogResult枚舉,該枚舉永遠不能等於“ true”。
我敢打賭,您的代碼的原始版本在使用SelectedItem
任何地方都使用SelectedIndex
,除非您確實在做可怕的事情。
另外,天哪,您是否需要使用數據綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.