Beispiel #1
0
        protected virtual Task OnSaveUserProperties()
        {
            FilePath file      = GetPreferencesFileName();
            var      userProps = userProperties;

            return(Task.Run(() => {
                if (userProps == null || userProps.IsEmpty)
                {
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                    }
                    return;
                }

                XmlTextWriter writer = null;
                try {
                    Directory.CreateDirectory(file.ParentDirectory);

                    writer = new XmlTextWriter(file, System.Text.Encoding.UTF8);
                    writer.Formatting = Formatting.Indented;
                    XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
                    ser.SerializationContext.BaseFile = file;
                    ser.Serialize(writer, userProps, typeof(PropertyBag));
                } catch (Exception e) {
                    LoggingService.LogWarning("Could not save solution preferences: " + file, e);
                } finally {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }));
        }
 public void SaveContents(string fileName)
 {
     using (StreamWriter writer = new StreamWriter(fileName))
     {
         XmlDataSerializer serializer = new XmlDataSerializer(MonoDevelop.Projects.Services.ProjectService.DataContext);
         serializer.Serialize(writer, this);
     }
 }
 public void SaveViews()
 {
     try {
         XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
         ser.Serialize(ConfigFile, views);
     } catch (Exception ex) {
         LoggingService.LogError("Error while saving monitor-views.xml", ex);
     }
 }
        public void XmlDataSerializer_SerializeXmlData_Success()
        {
            // Arrange
            var data =
                @"<?xml version=""1.0"" encoding=""utf-16""?><MockCountry xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Alpha2Code>mt</Alpha2Code></MockCountry>";

            var mockCountry = new MockCountry()
            {
                Alpha2Code = "mt"
            };

            // Act
            var value = _sut.Serialize(mockCountry);

            // Assert
            Assert.NotNull(data);
            Assert.Equal(data, value.Replace("\r", "").Replace("\n", ""));
        }
 static void SaveRuntimes()
 {
     try {
         XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
         using (StreamWriter sw = new StreamWriter(configFile)) {
             ser.Serialize(sw, customRuntimes);
         }
     } catch {
     }
 }
 public static void SaveConfiguration()
 {
     if (configuration != null)
     {
         XmlDataSerializer ser = new XmlDataSerializer(dataContext);
         using (var tw = new XmlTextWriter(File.CreateText(ConfigFile))) {
             tw.Formatting = Formatting.Indented;
             ser.Serialize(tw, configuration, typeof(VersionControlConfiguration));
         }
     }
 }
Beispiel #7
0
 public static void SaveConfig()
 {
     if (configuration != null)
     {
         XmlDataSerializer s = new XmlDataSerializer(dataContext);
         using (var wr = new XmlTextWriter(File.CreateText(ConfigFile)))
         {
             wr.Formatting = Formatting.Indented;
             s.Serialize(wr, configuration, typeof(AddinConfig));
         }
     }
 }
Beispiel #8
0
 static void SaveConfig()
 {
     try {
         XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
         StreamWriter      sw  = new StreamWriter(configFile);
         using (sw) {
             ser.Serialize(new XmlTextWriter(sw), config, typeof(AddinAuthoringServiceConfig));
         }
     }
     catch (Exception ex) {
         LoggingService.LogError("Could not save add-in authoring service configuration", ex);
     }
 }
 public static void SaveConfiguration()
 {
     if (configuration != null)
     {
         XmlDataSerializer ser = new XmlDataSerializer(dataContext);
         XmlTextWriter     tw  = new XmlTextWriter(new StreamWriter(ConfigFile));
         try {
             ser.Serialize(tw, configuration, typeof(VersionControlConfiguration));
         } finally {
             tw.Close();
         }
     }
 }
Beispiel #10
0
    static public void Serialize(IDictionary <A, B> dictionary, string filePath)
    {
        List <Item> itemList = new List <Item>();

        foreach (A key in dictionary.Keys)
        {
            itemList.Add(new Item()
            {
                Key = key, Value = dictionary[key]
            });
        }
        XmlDataSerializer.Serialize <List <Item> >(itemList, filePath);
    }
Beispiel #11
0
        internal static void InternalStoreRepositoryReference(Repository repo, string path, string id)
        {
            string file = Path.Combine(path, id) + ".mdvcs";

            XmlDataSerializer ser = new XmlDataSerializer(dataContext);
            XmlTextWriter     tw  = new XmlTextWriter(new StreamWriter(file));

            try {
                ser.Serialize(tw, repo, typeof(Repository));
            } finally {
                tw.Close();
            }
        }
        public void SerializeObject(bool serialize, int size)
        {
            var order = new MainDataOne();

            order.Name = "Test";
            var serializerTests = new XmlDataSerializer(serialize);
            var data            = serializerTests.Serialize(order);
            var orderResult     = (IMainData)serializerTests.Deserialize(typeof(MainDataOne), data);

            Assert.AreNotSame(order, orderResult);
            Assert.AreEqual(size, data.Length);
            Assert.AreEqual("Test", orderResult.Name);
        }
Beispiel #13
0
 static void SaveGlobalCustomExecutionModes()
 {
     if (globalModes == null)
     {
         return;
     }
     try {
         XmlDataSerializer ser  = new XmlDataSerializer(GetDataContext());
         FilePath          file = UserProfile.Current.ConfigDir.Combine("custom-command-modes.xml");
         ser.Serialize(file, globalModes, typeof(CustomExecutionModes));
     } catch (Exception ex) {
         LoggingService.LogError("Could not save global custom execution modes.", ex);
     }
 }
Beispiel #14
0
        public string LoadItems(string asmName, string typeName, string fileName)
        {
            XmlDataSerializer       ser    = new XmlDataSerializer(MonoDevelop.Projects.Services.ProjectService.DataContext);
            ToolboxList             tl     = new ToolboxList();
            object                  ob     = Activator.CreateInstance(asmName, typeName).Unwrap();
            IExternalToolboxLoader  loader = (IExternalToolboxLoader)ob;
            IList <ItemToolboxNode> list   = loader.Load(fileName);

            tl.AddRange(list);
            StringWriter sw = new StringWriter();

            ser.Serialize(sw, tl);
            return(sw.ToString());
        }
        public void Save()
        {
            XmlDataSerializer ser = new XmlDataSerializer(IdeApp.Services.ProjectService.DataContext);

            try {
                using (StreamWriter sw = new StreamWriter(ToolboxIndexFile)) {
                    ser.Serialize(sw, this, typeof(ComponentIndex));
                }
            }
            catch (Exception ex) {
                // Ignore exceptions
                LoggingService.LogError(ex.ToString());
            }
        }
Beispiel #16
0
            public async Task WhenContentIsXml_ThenReturnObj()
            {
                var dummy = new TestDummy {
                    Name = "John"
                };

                var xml = XmlDataSerializer.Serialize(dummy);

                var sut = new StringContent(xml);

                var result = await sut.ReadAsXmlAsync <TestDummy>();

                Assert.That(result.Name, Is.EqualTo(dummy.Name));
            }
        void WriteSolutionEntityItem(FilePath actualFile, FilePath outFile, object node, IProgressMonitor monitor)
        {
            StreamWriter sw = new StreamWriter(outFile);

            try {
                monitor.BeginTask(string.Format(GettextCatalog.GetString("Saving solution item: {0}"), actualFile), 1);
                XmlDataSerializer ser = new XmlDataSerializer(MD1ProjectService.DataContext);
                ser.SerializationContext.BaseFile        = actualFile;
                ser.SerializationContext.ProgressMonitor = monitor;
                ser.Serialize(sw, node, typeof(SolutionEntityItem));
            } catch (Exception ex) {
                monitor.ReportError(string.Format(GettextCatalog.GetString("Could not save solution item: {0}"), actualFile), ex);
            } finally {
                monitor.EndTask();
                sw.Close();
            }
        }
        void WriteProject(FilePath actualFile, FilePath outFile, Project project, IProgressMonitor monitor)
        {
            StreamWriter sw = new StreamWriter(outFile);

            try {
                monitor.BeginTask(GettextCatalog.GetString("Saving project: {0}", actualFile), 1);
                XmlDataSerializer ser = new XmlDataSerializer(MD1ProjectService.DataContext);
                ser.SerializationContext.BaseFile        = actualFile;
                ser.SerializationContext.ProgressMonitor = monitor;
                ser.Serialize(sw, project, typeof(Project));
            } catch (Exception ex) {
                monitor.ReportError(GettextCatalog.GetString("Could not save project: {0}", actualFile), ex);
                throw;
            } finally {
                monitor.EndTask();
                sw.Close();
            }
        }
Beispiel #19
0
        internal void SaveGeneratedFrameworkInfo()
        {
            if (GeneratedFrameworksFile != null)
            {
                Console.WriteLine("Saving frameworks file: " + GeneratedFrameworksFile);
                using (StreamWriter sw = new StreamWriter(GeneratedFrameworksFile)) {
                    XmlTextWriter tw = new XmlTextWriter(sw);
                    tw.Formatting = Formatting.Indented;
                    XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
                    ser.Serialize(tw, frameworks);
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(GeneratedFrameworksFile);
                doc.DocumentElement.InsertBefore(doc.CreateComment("This file has been autogenerated. DO NOT MODIFY!"), doc.DocumentElement.FirstChild);
                doc.Save(GeneratedFrameworksFile);
            }
        }
        void WriteWorkspaceItem(FilePath actualFile, FilePath outFile, WorkspaceItem item, IProgressMonitor monitor)
        {
            Workspace ws = item as Workspace;

            if (ws != null)
            {
                monitor.BeginTask(null, ws.Items.Count);
                try
                {
                    foreach (WorkspaceItem it in ws.Items)
                    {
                        it.Save(monitor);
                        monitor.Step(1);
                    }
                }
                finally
                {
                    monitor.EndTask();
                }
            }

            StreamWriter sw = new StreamWriter(outFile);

            try
            {
                monitor.BeginTask(GettextCatalog.GetString("Saving item: {0}", actualFile), 1);
                XmlTextWriter tw = new XmlTextWriter(sw);
                tw.Formatting = Formatting.Indented;
                XmlDataSerializer ser = new XmlDataSerializer(MD1ProjectService.DataContext);
                ser.SerializationContext.BaseFile        = actualFile;
                ser.SerializationContext.ProgressMonitor = monitor;
                ser.Serialize(sw, item, typeof(WorkspaceItem));
            }
            catch (Exception ex)
            {
                monitor.ReportError(GettextCatalog.GetString("Could not save item: {0}", actualFile), ex);
                throw;
            }
            finally
            {
                monitor.EndTask();
                sw.Close();
            }
        }
Beispiel #21
0
        internal static void SaveUserTasks(IWorkspaceObject item)
        {
            string fileToSave = GetUserTasksFilename((WorkspaceItem)item);

            try {
                List <Task> utasks = new List <Task> (userTasks.GetItemTasks(item, true));
                if (utasks.Count == 0)
                {
                    if (File.Exists(fileToSave))
                    {
                        File.Delete(fileToSave);
                    }
                }
                else
                {
                    XmlDataSerializer serializer = new XmlDataSerializer(new DataContext());
                    serializer.Serialize(fileToSave, utasks);
                }
            } catch (Exception ex) {
                LoggingService.LogWarning("Could not save user tasks: " + fileToSave, ex);
            }
        }
Beispiel #22
0
        public virtual void SaveUserProperties()
        {
            PropertyBag userProperties      = this.UserProperties;
            string      preferencesFileName = this.GetPreferencesFileName();

            if (this.userProperties == null || this.userProperties.IsEmpty)
            {
                if (!File.Exists(preferencesFileName))
                {
                    return;
                }
                File.Delete(preferencesFileName);
            }
            else
            {
                XmlTextWriter xmlTextWriter = (XmlTextWriter)null;
                try
                {
                    xmlTextWriter            = new XmlTextWriter(preferencesFileName, Encoding.UTF8);
                    xmlTextWriter.Formatting = Formatting.Indented;
                    XmlDataSerializer ser = new XmlDataSerializer(new DataContext());
                    this.DataContextIncludeType(ser);
                    ser.SerializationContext.BaseFile = preferencesFileName;
                    ser.Serialize((XmlWriter)xmlTextWriter, (object)this.userProperties, typeof(PropertyBag));
                }
                catch (Exception ex)
                {
                    LoggingService.LogWarning("Could not save solution preferences: " + this.GetPreferencesFileName(), ex);
                }
                finally
                {
                    if (xmlTextWriter != null)
                    {
                        xmlTextWriter.Close();
                    }
                }
            }
        }
        public void DeserializeTest()
        {
            // first delete the file if it already exists
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            // serialize
            xmlSerializer.Serialize(context);

            var newContext = new DataContext();

            xmlSerializer.Deserialize(ref newContext);

            // check collection sizes
            Assert.AreEqual(context.bookReaders.Count, newContext.bookReaders.Count);
            Assert.AreEqual(context.bookStates.Count, newContext.bookStates.Count);
            Assert.AreEqual(context.books.Count, newContext.books.Count);
            Assert.AreEqual(context.events.Count, newContext.events.Count);

            // check if collections contain objects with the same properties
            foreach (var book in context.books.Values)
            {
                Assert.IsTrue(newContext.books.ContainsValue(book));
            }

            foreach (var bookState in context.bookStates)
            {
                Assert.IsTrue(newContext.bookStates.Contains(bookState));
            }

            foreach (var bookReader in context.bookReaders)
            {
                Assert.IsTrue(newContext.bookReaders.Contains(bookReader));
            }

            foreach (var e in context.events)
            {
                Assert.IsTrue(newContext.events.Contains(e));
            }

            // check book references in book states
            foreach (var book in newContext.books.Values)
            {
                var bookStates = newContext.bookStates.FindAll(bs => bs.Book.Equals(book));

                if (bookStates.Count > 0)
                {
                    foreach (var bookState in bookStates)
                    {
                        Assert.IsTrue(object.ReferenceEquals(book, bookState.Book));
                    }
                }
            }

            // check book state references in events
            foreach (var bookState in newContext.bookStates)
            {
                var events = newContext.events.Where(e => e.BookState.Equals(bookState)).ToList();

                if (events.Count > 0)
                {
                    foreach (var ev in events)
                    {
                        Assert.IsTrue(object.ReferenceEquals(bookState, ev.BookState));
                    }
                }
            }

            // check book reader references in events
            foreach (var bookReader in newContext.bookReaders)
            {
                var events = newContext.events.Where(e => e.BookReader.Equals(bookReader)).ToList();

                if (events.Count > 0)
                {
                    foreach (var ev in events)
                    {
                        Assert.IsTrue(object.ReferenceEquals(bookReader, ev.BookReader));
                    }
                }
            }
        }
Beispiel #24
0
        public static string Serialize(object entity, string rootElementName)
        {
            XmlDataSerializer serializer = new XmlDataSerializer();

            return(serializer.Serialize(entity, rootElementName));
        }
Beispiel #25
0
        public static string Serialize(object entity)
        {
            XmlDataSerializer serializer = new XmlDataSerializer();

            return(serializer.Serialize(entity));
        }
Beispiel #26
0
        public void Serialize_MySerializableEntityFilledWithValidData_ShouldBeEqualsWithXmlContentFrom_MySerializableEntityTest1Data()
        {
            MySerializableEntity mySerializableEntityChild = new MySerializableEntity("Ctor argument11", "Ctor argument22");

            mySerializableEntityChild.Value0     = "Value00";
            mySerializableEntityChild.Value2     = 2;
            mySerializableEntityChild.Value19    = 4;
            mySerializableEntityChild.Value18    = "This is my attr value";
            mySerializableEntityChild.BaseValue1 = "base Value11";
            mySerializableEntityChild.Value28    = 28;
            mySerializableEntityChild.Value29    = 29;
            mySerializableEntityChild.Value30    = 30;
            mySerializableEntityChild.Value31    = 31;
            mySerializableEntityChild.Value32    = new MySerializableEntity2 {
                Value1 = 10
            };

            MySerializableEntity mySerializableEntityChildChild = new MySerializableEntity("Ctor argument111", "Ctor argument222");

            mySerializableEntityChildChild.Value0     = "Value000";
            mySerializableEntityChildChild.Value2     = 2;
            mySerializableEntityChildChild.Value10    = string.Empty;
            mySerializableEntityChildChild.BaseValue1 = "base Value111";
            mySerializableEntityChildChild.Value28    = 28;
            mySerializableEntityChildChild.Value29    = 29;
            mySerializableEntityChildChild.Value30    = 30;
            mySerializableEntityChildChild.Value31    = 31;
            mySerializableEntityChild.Value32         = new MySerializableEntity2 {
                Value1 = 11
            };

            mySerializableEntityChild.Child = mySerializableEntityChildChild;


            MySerializableEntity2 mySerializableEntity2 = new MySerializableEntity2 {
                Value1 = 20
            };


            MySerializableEntity mySerializableEntity = new MySerializableEntity("Ctor argument1", "Ctor argument2");

            mySerializableEntity.Value0 = "Value0";
            mySerializableEntity.Value1 = 1;
            mySerializableEntity.Value2 = 2;
            mySerializableEntity.Value3 = 3;
            mySerializableEntity.Value4 = new[] { 1, 2, 3, 4 };
            mySerializableEntity.Value5 = new List <string>
            {
                "11 11",
                "22 22"
            };



            mySerializableEntity.Value6 = new object[] { 1, 0.5, mySerializableEntityChild, 33, mySerializableEntity.Value4, new object[] { 1, "test", 3, new object[] { "test", 2, 3, new List <int> {
                                                                                                                                                                             10, 13, 14
                                                                                                                                                                         }, 4 }, 4 }, "test", mySerializableEntity2 };
            mySerializableEntity.Value7 = new List <string>
            {
                "33 44",
                "55 66"
            };

            mySerializableEntity.Value8    = new object[] { 1, 0.5, 33, mySerializableEntityChild, "test" };
            mySerializableEntity.Value9    = new object[4];
            mySerializableEntity.Value9[2] = mySerializableEntityChild;

            mySerializableEntity.Value10 = string.Empty;
            mySerializableEntity.Child   = mySerializableEntityChild;

            mySerializableEntity.Value11 = new ArrayList {
                10, "Test"
            };

            mySerializableEntity.Value12 = new List <IReadOnlyCollection <List <IEnumerable <object[]> > > >();
            var complexList = new List <IReadOnlyCollection <List <IEnumerable <object[]> > > >();

            var subList       = new List <List <IEnumerable <object[]> > > ();
            var subSubList    = new List <IEnumerable <object[]> >();
            var subSubSubList = new List <object[]>();


            object[] values = { "aaa", "bbb", 3 };

            subSubSubList.Add(values);
            subSubSubList.Add(values);


            subSubList.Add(subSubSubList);
            subSubList.Add(new List <object[]>());
            subSubList.Add(new List <object[]>());

            subList.Add(subSubList);
            subList.Add(subSubList);

            complexList.Add(subList);
            complexList.Add(subList);
            mySerializableEntity.Value12 = complexList;

            mySerializableEntity.Value13 = new HashSet <int> {
                21, 31
            };
            mySerializableEntity.Value14 = new Collection <int> {
                41, 51
            };
            mySerializableEntity.Value15 = new Queue <int>(new [] { 61, 71 });
            mySerializableEntity.Value16 = new Stack <int>(new[] { 91, 81 });
            mySerializableEntity.Value17 = new List <int>(new[] { 100, 101 }).AsReadOnly();
            mySerializableEntity.Value18 = "This is an attribute value";
            mySerializableEntity.Value19 = 10;

            mySerializableEntity.Value22.Add("test1");
            mySerializableEntity.Value22.Add("test2");
            mySerializableEntity.Value22.Add("test3");
            mySerializableEntity.Value23 = new[] { "test1", "test2" };

            mySerializableEntity.Value28 = 28;
            mySerializableEntity.Value29 = 29;
            mySerializableEntity.Value30 = 30;
            mySerializableEntity.Value31 = 31;


            mySerializableEntity.BaseValue1 = "base Value1";

            mySerializableEntity.Value32 = mySerializableEntity2;


            XmlDataSerializer xmlSerializer      = new XmlDataSerializer(_xmlModelTypeBuilder);
            XDocument         serializedInstance = xmlSerializer.Serialize(mySerializableEntity);

            string xmlOutput = serializedInstance.ToString();

            Assert.AreEqual(Resources.MySerializableEntityTest1Data, xmlOutput);
        }