Example #1
0
        public void ShouldThrowArgumentNullExceptionIfObjectIsNull()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;

            // Act
            var serializedObject   = xmlSerializerHelper.SerializeToXml <object>(null);
            var deserializedObject = xmlSerializerHelper.DeserializeFromXml <object>(serializedObject);

            // Assert
            Assert.That(serializedObject, Is.Not.Null);
            Assert.That(deserializedObject, Is.Null);
        }
Example #2
0
        public void ShouldSerializeEmptyObject()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;
            object obj = new object();

            // Act
            var serializedString   = xmlSerializerHelper.SerializeToXml(obj);
            var deserializedObject = xmlSerializerHelper.DeserializeFromXml <object>(serializedString);

            // Assert
            Assert.That(serializedString, Is.Not.Null);
            Assert.That(deserializedObject, Is.Not.Null);
        }
Example #3
0
        public void ShouldSerializeNullableValue()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;

            // Act
            // 观察 preserveTypeInformation 参数 记录非接口 的真实类型
            var serializedString = xmlSerializerHelper.SerializeToXml <int?>(null, preserveTypeInformation: true);
            var deserialized     = xmlSerializerHelper.DeserializeFromXml <int?>(serializedString);

            // Assert
            Assert.That(serializedString, Is.Not.Null);
            Assert.That(deserialized, Is.Null);
        }
Example #4
0
        public void ShouldSerializeSimpleObjectWithGenericMethod()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;
            object inputObject = new SimpleSerializerClass {
                BoolProperty = true, StringProperty = "test"
            };

            // Act
            var serializedString   = xmlSerializerHelper.SerializeToXml(inputObject);
            var deserializedObject = xmlSerializerHelper.DeserializeFromXml <SimpleSerializerClass>(serializedString);

            // Assert
            Assert.That(serializedString, Is.Not.Null);
            Assert.That(deserializedObject, Is.Not.Null);
            Assert.That(inputObject, Is.EqualTo(deserializedObject));
        }
Example #5
0
        public void ShouldSerializeConcreteList()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;
            List <string>        inputList           = new List <string> {
                "a", "b", "c"
            };

            // Act
            var serializedString = xmlSerializerHelper.SerializeToXml(inputList);
            var deserializedList = xmlSerializerHelper.DeserializeFromXml <List <string> >(serializedString);

            // Assert
            Assert.That(serializedString, Is.Not.Null);
            Assert.That(deserializedList, Is.Not.Null);
            Assert.That(deserializedList, Has.Count);
            Assert.That(inputList, Is.EquivalentTo(deserializedList));
        }
Example #6
0
        public void ShouldSerializeInterfaceList()
        {
            // Arrange
            IXmlSerializerHelper xmlSerializerHelper = XmlSerializerHelper.Instance;
            IList <string>       inputList           = new List <string> {
                "a", "b", "c"
            };

            // Act
            // preserveTypeInformation 记录 接口类型 的真实类型 认真调试下本条
            var serializedString = xmlSerializerHelper.SerializeToXml(inputList, preserveTypeInformation: true);
            var deserializedList = xmlSerializerHelper.DeserializeFromXml <IList <string> >(serializedString);

            // Assert
            Assert.That(serializedString, Is.Not.Null);
            Assert.That(deserializedList, Is.Not.Null);
            Assert.That(deserializedList, Has.Count);
            Assert.That(inputList, Is.EquivalentTo(deserializedList));
        }
Example #7
0
        private void button_Save_Click(object sender, EventArgs e)
        {
            var modsPath    = listBox_Mods.SelectedItem.ToString();
            var resModsPath = listBox_ResMods.SelectedItem.ToString();

            modsPath    = modsPath.Split('\\').Last();
            resModsPath = resModsPath.Split('\\').Last();

            var pathsModel = GetPathXmlModel(textBox_GameDirectory.Text);
            var dirPaths   = pathsModel
                             .Paths
                             .Items
                             .Select(i => i as PathsXmlModel.rootPathsPath)
                             .Where(i => i != null)
                             .ToList();


            var ModsDir = dirPaths
                          .SingleOrDefault(i => i.Value.StartsWith("./mods/"));
            var ResModsDir = dirPaths
                             .SingleOrDefault(i => i.Value.StartsWith("./res_mods/"));

            ModsDir.Value    = "./mods/" + modsPath;
            ResModsDir.Value = "./res_mods/" + resModsPath;

            var xmlString = xmlSerializerHelper.SerializeToXml <PathsXmlModel.root>(pathsModel, encoding: Encoding.ASCII);

            xmlString = xmlString
                        .Replace("<?xml version=\"1.0\" encoding=\"us-ascii\"?>", "")
                        .Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "")
                        .Trim();

            var pathXmlPath = PathsXmlPath(textBox_GameDirectory.Text);

            File.Copy(pathXmlPath, Path.Combine(textBox_GameDirectory.Text, "paths.xml.old"), overwrite: true);
            File.WriteAllText(pathXmlPath, xmlString);
        }
Example #8
0
 private string SerializeListToXml()
 {
     return(xmlSerializerHelper.SerializeToXml(listxml, encoding: Encoding.ASCII));
 }