hi i have the following code to perform xml serialization:
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);
}
}
the error "There was an error generating the XML document" occurs at serializer.Serialize(savestream, abc);
my form saving class:
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;
}
}
can anyone help me fix this?
EDIT:
i tried this but it doesnt work as well:
under "save button"
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem.ToString();
under "load button"
StartzbuildfromcomboBox.SelectedItem = abc.Startzbuildfrom;
here is how i populate my combobox items:
<ComboBox Height="23" Margin="577,72,497,0" Name="StartzbuildfromcomboBox" VerticalAlignment="Top"><ComboBoxItem>library</ComboBoxItem></ComboBox>
What is the data source for StartzbuildcomboBox?
And more specifically what is the type of DataItem of each StartzbuildcomboBox.SelectedItem?
Can you also include the InnerException?
Most possible reason could be that Startzbuildfrom (shouldn't it be StartzBuildFrom?) is assigned to a type which the XmlSerializer has no idea about.
If you know the type then decorate FormSaving with XmlInclude.
[XmlInclude(typeof(type-of-selected-combobox-selected-item))]
public class FormSaving
{
.........
Although object
is technically a serializable type, the concrete type of Startzbuildfrom is obscured. In fact, you are trying to serialize a ComboBoxItem, which is not serializable. Try using a serializable type for the Startzbuildfrom property and set its value using the SelectedValue property of the combo box, not the SelectedItem property.
Ok solved,
i tried this:
public class FormSaving
{
...
public int Startzbuildfrom
{
get;
set;
}
}
...
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedIndex;
...
StartzbuildfromcomboBox.SelectedIndex = abc.Startzbuildfrom;
精彩评论