您好我有以下代碼來執行XML序列化:時發生錯誤,生成XML文檔
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:/";
DialogSave.ShowDialog();
savepath = DialogSave.FileName;
DialogSave.Dispose();
DialogSave = null;
FormSaving abc = new FormSaving();
if (MajorversionresultLabel != null && MajorversionresultLabel.Content != null && MajorversionLabel.Content.ToString() != string.Empty)
abc.Majorversion = MajorversionresultLabel.Content.ToString();
//abc.Minorversion = MinorversionresultLabel.Content.ToString();
//abc.Projectnumber = ProjectnumberresultLabel.Content.ToString();
//abc.Buildnumber = BuildnumberresultLabel.Content.ToString();
//abc.Previousbuildversion = PreviousbuildversionresultLabel.Content.ToString();
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem;
using (Stream savestream = new FileStream(savepath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
serializer.Serialize(savestream, abc);
}
}
錯誤「時發生錯誤,生成XML文檔」在serializer.Serialize(savestream, abc);
我的形式發生節能等級:
public class FormSaving
{
public string Majorversion
{
get;
set;
}
public string Minorversion
{
get;
set;
}
public string Projectnumber
{
get;
set;
}
public string Buildnumber
{
get;
set;
}
public string Previousbuildversion
{
get;
set;
}
public object Startzbuildfrom
{
get;
set;
}
}
任何人都可以幫我解決這個問題嗎?
編輯:
我試過,但它不工作,以及:在 「Load按鈕」
StartzbuildfromcomboBox.SelectedItem = abc.Startzbuildfrom;
這裏是
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem.ToString();
下的 「保存按鈕」
如何填充我的組合框項目:
<ComboBox Height="23" Margin="577,72,497,0" Name="StartzbuildfromcomboBox" VerticalAlignment="Top"><ComboBoxItem>library</ComboBoxItem></ComboBox>
是FormSaving您已經定義了一個自定義類?我以前沒有聽說過,也沒有在Google上找到任何關於它的參考。如果是這樣,你能否請張貼它,或者如果沒有,給我一個鏈接,我可以找到更多的信息。謝謝。 – joshhendo 2011-02-24 06:01:58
@joshhendo嗨,我剛剛編輯添加類。謝謝 – jeremychan 2011-02-24 06:11:40
減去1.答案沒有被標記爲正確的,在對許多代碼行進行排序之後,很清楚發生了什麼,但是這是一個很好的SO問題,答案應該清楚地表明對問題的洞察力和清晰的解決方案。這整篇文章的最大問題是,一個人在這裏跟隨鏈接將很難找出如何解決這個問題。沒有這些變化,這篇文章對SO知識庫幾乎沒有什麼影響,並且增加了比內容更多的噪音。 – MedicineMan 2011-02-25 01:55:54