您好,我有以下代码来执行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);
}
}
在serializer.Serialize(savestream, abc);
中出现“生成XML文档时出错”的错误
我的表单保存类:
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;
}
}
有人能帮我解决这个问题吗?
编辑:
我试过了,但效果不太好:
在“保存按钮”下
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem.ToString();
在“加载按钮”下
StartzbuildfromcomboBox.SelectedItem = abc.Startzbuildfrom;
下面是我如何填充我的combobox项:
<ComboBox Height="23" Margin="577,72,497,0" Name="StartzbuildfromcomboBox" VerticalAlignment="Top"><ComboBoxItem>library</ComboBoxItem></ComboBox>
https://stackoverflow.com/questions/5100853
复制相似问题