Esempio n. 1
0
        private void DeserializeButton_Click(object sender, EventArgs e)
        {
            int index = serializeBox.SelectedIndex;

            switch (index)
            {
            case 0:                                         //binary
                var bf = new BinaryFormatter {
                    Binder = new MyBinaryBinder(listOfTypes)
                };
                using (var binaryFileStream = new FileStream("binary.txt", FileMode.Open))
                {
                    list.Clear();
                    list = (List <Weapons>)bf.Deserialize(binaryFileStream);
                }
                break;

            case 1:                                         //xml
                Type[] extraTypes    = listOfClasses.ToArray();
                var    xmlSerializer = new XmlSerializer(typeof(List <Weapons>), extraTypes);
                using (var xmlFileStream = new FileStream("xml.txt", FileMode.Open))
                {
                    var    stringWriter = new StringWriter();
                    var    streamReader = new StreamReader(xmlFileStream);
                    string fullText     = streamReader.ReadToEnd();
                    list.Clear();

                    string key = jsonBox.SelectedItem.ToString();
                    Type   jsonConverterType = converters[key];
                    var    jsonConverter     = (IConverter)Activator.CreateInstance(jsonConverterType);
                    stringWriter.Write(jsonConverter.ConvertInput(fullText));
                    var stringReader = new StringReader(stringWriter.GetStringBuilder().ToString());
                    list = (List <Weapons>)xmlSerializer.Deserialize(stringReader);
                }
                break;

            case 2:
                var userSerializer = new MySerializer();
                using (var userFileStream = new FileStream("user.txt", FileMode.Open))
                {
                    var streamReader = new StreamReader(userFileStream);
                    list.Clear();
                    list = (List <Weapons>)userSerializer.Deserialize(streamReader, listOfTypes);
                }
                break;
            }

            RefreshTree();
        }
Esempio n. 2
0
        private void SerializeButton_Click(object sender, EventArgs e)
        {
            int index = serializeBox.SelectedIndex;

            switch (index)
            {
            case 0:                                     //binary
                var bf = new BinaryFormatter {
                    Binder = new MyBinaryBinder(listOfTypes)
                };
                using (var binaryFileStream = new FileStream("binary.txt", FileMode.Create))
                {
                    bf.Serialize(binaryFileStream, list);
                }
                break;

            case 1:                                     //XML
                Type[] extraTypes    = listOfClasses.ToArray();
                var    xmlSerializer = new XmlSerializer(typeof(List <Weapons>), extraTypes);
                using (var xmlFileStream = new FileStream("xml.txt", FileMode.Create))
                {
                    var stringWriter = new StringWriter();
                    xmlSerializer.Serialize(stringWriter, list);

                    string key = jsonBox.SelectedItem.ToString();
                    Type   jsonConverterType = converters[key];
                    var    jsonConverter     = (IConverter)Activator.CreateInstance(jsonConverterType);
                    var    fileWriterEmpty   = new StringWriter();
                    fileWriterEmpty.Write(jsonConverter.ConvertOutput(stringWriter.GetStringBuilder().ToString()));
                    for (int i = 0; i < fileWriterEmpty.GetStringBuilder().Length; i++)
                    {
                        xmlFileStream.WriteByte((byte)fileWriterEmpty.GetStringBuilder()[i]);
                    }
                }
                break;

            case 2:                                     //user
                var userSerializer = new MySerializer();
                using (var userFileStream = new FileStream("user.txt", FileMode.Create))
                {
                    var streamWriter = new StreamWriter(userFileStream)
                    {
                        AutoFlush = true
                    };
                    userSerializer.Serialize(streamWriter, list);
                }
                break;
            }
        }
Esempio n. 3
0
        private void SerializeButton_Click(object sender, EventArgs e)
        {
            int index = serializeBox.SelectedIndex;
            switch (index)
            {
                case 0:                                 //binary
                    var bf = new BinaryFormatter { Binder = new MyBinaryBinder(listOfTypes) };
                    using (var binaryFileStream = new FileStream("binary.txt", FileMode.Create))
                    {
                        bf.Serialize(binaryFileStream, list);
                    }
                    break;
                case 1:                                 //XML
                    Type[] extraTypes = listOfClasses.ToArray();
                    var xmlSerializer = new XmlSerializer(typeof(List<Weapons>), extraTypes);
                    using (var xmlFileStream = new FileStream("xml.txt", FileMode.Create))
                    {
                        var stringWriter = new StringWriter();
                        xmlSerializer.Serialize(stringWriter, list);

                        string key = jsonBox.SelectedItem.ToString();
                        Type jsonConverterType = converters[key];
                        var jsonConverter = (IConverter)Activator.CreateInstance(jsonConverterType);
                        var fileWriterEmpty = new StringWriter();
                        fileWriterEmpty.Write(jsonConverter.ConvertOutput(stringWriter.GetStringBuilder().ToString()));
                        for (int i = 0; i < fileWriterEmpty.GetStringBuilder().Length; i++)
                            xmlFileStream.WriteByte((byte)fileWriterEmpty.GetStringBuilder()[i]);
                    }
                    break;
                case 2:                                 //user
                    var userSerializer = new MySerializer();
                    using (var userFileStream = new FileStream("user.txt", FileMode.Create))
                    {
                        var streamWriter = new StreamWriter(userFileStream) { AutoFlush = true };
                        userSerializer.Serialize(streamWriter, list);
                    }
                    break;
            }

        }
Esempio n. 4
0
        private void DeserializeButton_Click(object sender, EventArgs e)
        {
            int index = serializeBox.SelectedIndex;
            switch (index)
            {
                case 0:                                     //binary
                    var bf = new BinaryFormatter { Binder = new MyBinaryBinder(listOfTypes) };
                    using (var binaryFileStream = new FileStream("binary.txt", FileMode.Open))
                    {
                        list.Clear();
                        list = (List<Weapons>)bf.Deserialize(binaryFileStream);
                    }
                    break;
                case 1:                                     //xml
                    Type[] extraTypes = listOfClasses.ToArray();
                    var xmlSerializer = new XmlSerializer(typeof(List<Weapons>), extraTypes);
                    using (var xmlFileStream = new FileStream("xml.txt", FileMode.Open))
                    {
                        var stringWriter = new StringWriter();
                        var streamReader = new StreamReader(xmlFileStream);
                        string fullText = streamReader.ReadToEnd();
                        list.Clear();

                        string key = jsonBox.SelectedItem.ToString();
                        Type jsonConverterType = converters[key];
                        var jsonConverter = (IConverter)Activator.CreateInstance(jsonConverterType);
                        stringWriter.Write(jsonConverter.ConvertInput(fullText));
                        var stringReader = new StringReader(stringWriter.GetStringBuilder().ToString());
                        list = (List<Weapons>)xmlSerializer.Deserialize(stringReader);
                    }
                    break;
                case 2:
                    var userSerializer = new MySerializer();
                    using (var userFileStream = new FileStream("user.txt", FileMode.Open))
                    {
                        var streamReader = new StreamReader(userFileStream);
                        list.Clear();
                        list = (List<Weapons>)userSerializer.Deserialize(streamReader, listOfTypes);

                    }
                    break;
            }

            RefreshTree();
        }