SerializeToFile() public method

Serializes the specified object to file.
public SerializeToFile ( object obj, string fileName ) : void
obj object The object to serialize.
fileName string Path to the file.
return void
Beispiel #1
0
        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());
            }
        }
Beispiel #2
0
        public void SaveState(string fileName, uint simulationStep)
        {
            try
            {
                string dataFolder = Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".statedata";

                MyNetworkState networkState = new MyNetworkState()
                {
                    ProjectName = Owner.Name,
                    MemoryBlocksLocation = dataFolder,
                    SimulationStep = simulationStep
                };

                YAXSerializer serializer = new YAXSerializer(typeof(MyNetworkState),
                    YAXExceptionHandlingPolicies.ThrowErrorsOnly,
                    YAXExceptionTypes.Warning);

                serializer.SerializeToFile(networkState, fileName);

                if (Directory.Exists(dataFolder))
                {
                    Directory.Delete(dataFolder, true);
                }

                Directory.CreateDirectory(dataFolder);
                MyMemoryManager.Instance.SaveBlocks(this, true, dataFolder);
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine("Saving state failed: " + e.Message);
            }
        }
Beispiel #3
0
        private void exportStateButton_Click(object sender, EventArgs e)
        {
            if (saveMemFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    string dataFolder = MyProject.MakeDataFolderFromFileName(saveMemFileDialog.FileName);
                    
                    MyMemoryBlockSerializer.ExportTempStorage(Project, dataFolder);

                    MyNetworkState networkState = new MyNetworkState()
                    {
                        ProjectName = Project.Name,
                        MemoryBlocksLocation = dataFolder,
                        SimulationStep = SimulationHandler.SimulationStep
                    };

                    YAXSerializer serializer = new YAXSerializer(typeof(MyNetworkState),
                        YAXExceptionHandlingPolicies.ThrowErrorsOnly,
                        YAXExceptionTypes.Warning);

                    serializer.SerializeToFile(networkState, saveMemFileDialog.FileName);
                    MyLog.INFO.WriteLine("Saving state: " + saveMemFileDialog.FileName);
                }
                catch (Exception ex)
                {
                    MyLog.ERROR.WriteLine("Saving state failed: " + ex.Message);
                }    
            }
        }
Beispiel #4
0
 public void Save()
 {
     var serializer = new YAXSerializer(typeof(NuspecModel), YAXExceptionHandlingPolicies.ThrowWarningsAndErrors,
         YAXExceptionTypes.Error, YAXSerializationOptions.DontSerializeNullObjects);
     serializer.SerializeToFile(model, fileLocation);
 }