private void OnSerialize(bool saveToFile) { object selItem = lstSampleClasses.SelectedItem; if (selItem == null || !(selItem is ClassInfoListItem)) { return; } string fileName = null; if (saveToFile) { if (DialogResult.OK != saveFileDialog1.ShowDialog()) { return; } fileName = saveFileDialog1.FileName; } ClassInfoListItem info = selItem as ClassInfoListItem; YAXExceptionTypes defaultExType = GetSelectedDefaultExceptionType(); YAXExceptionHandlingPolicies exPolicy = GetSelectedExceptionHandlingPolicy(); YAXSerializationOptions serOption = GetSelectedSerializationOption(); try { YAXSerializer serializer = new YAXSerializer(info.ClassType, exPolicy, defaultExType, serOption); serializer.MaxRecursion = Convert.ToInt32(numMaxRecursion.Value); if (saveToFile) { serializer.SerializeToFile(info.SampleObject, fileName); } else { rtbXMLOutput.Text = serializer.Serialize(info.SampleObject); } rtbParsingErrors.Text = serializer.ParsingErrors.ToString(); } catch (YAXException ex) { MessageBox.Show("YAXException handled:\r\n\r\n" + ex.ToString()); } catch (Exception ex) { MessageBox.Show("Other Exception handled:\r\n\r\n" + ex.ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="YAXSerializer"/> class. /// </summary> /// <param name="t">The type of the object being serialized/deserialized.</param> /// <param name="exceptionPolicy">The exception handling policy.</param> /// <param name="defaultExType">The exceptions are treated as the value specified, unless otherwise specified.</param> /// <param name="option">The serialization option.</param> public YAXSerializer(Type t, YAXExceptionHandlingPolicies exceptionPolicy, YAXExceptionTypes defaultExType, YAXSerializationOptions option) { m_type = t; m_exceptionPolicy = exceptionPolicy; m_defaultExceptionType = defaultExType; m_serializationOption = option; // this must be the last call m_udtWrapper = TypeWrappersPool.Pool.GetTypeWrapper(m_type, this); if (m_udtWrapper.HasNamespace) TypeNamespace = m_udtWrapper.Namespace; }
private void OnDeserialize(bool openFromFile) { rtbParsingErrors.Text = ""; object selItem = lstSampleClasses.SelectedItem; if (selItem == null || !(selItem is ClassInfoListItem)) { return; } string fileName = null; if (openFromFile) { if (DialogResult.OK != openFileDialog1.ShowDialog()) { return; } fileName = openFileDialog1.FileName; } var info = selItem as ClassInfoListItem; YAXExceptionTypes defaultExType = GetSelectedDefaultExceptionType(); YAXExceptionHandlingPolicies exPolicy = GetSelectedExceptionHandlingPolicy(); YAXSerializationOptions serOption = GetSelectedSerializationOption(); try { object deserializedObject = null; YAXSerializer serializer = new YAXSerializer(info.ClassType, exPolicy, defaultExType, serOption); serializer.MaxRecursion = Convert.ToInt32(numMaxRecursion.Value); if (openFromFile) { deserializedObject = serializer.DeserializeFromFile(fileName); } else { deserializedObject = serializer.Deserialize(rtbXMLOutput.Text); } rtbParsingErrors.Text = serializer.ParsingErrors.ToString(); if (deserializedObject != null) { rtbDeserializeOutput.Text = deserializedObject.ToString(); if (deserializedObject is List <string> ) { StringBuilder sb = new StringBuilder(); foreach (var item in deserializedObject as List <string> ) { sb.AppendLine(item.ToString()); } MessageBox.Show(sb.ToString()); } } else { rtbDeserializeOutput.Text = "The deserialized object is null"; } } catch (YAXException ex) { rtbDeserializeOutput.Text = ""; MessageBox.Show("YAXException handled:\r\n\r\n" + ex.ToString()); } catch (Exception ex) { rtbDeserializeOutput.Text = ""; MessageBox.Show("Other Exception handled:\r\n\r\n" + ex.ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="YAXSerializer"/> class. /// </summary> /// <param name="type">The type of the object being serialized/deserialized.</param> /// <param name="seializationOptions">The serialization option flags.</param> public YAXSerializer(Type type, YAXSerializationOptions seializationOptions) : this(type, YAXExceptionHandlingPolicies.ThrowWarningsAndErrors, YAXExceptionTypes.Error, seializationOptions) { }