BinaryFormatter抛出异常,建议的解决方案不起作用

时间:2017-10-27 01:17:47

标签: c# wpf xamarin serialization deserialization

所以我一直在为学校作业构建一个应用程序,它是我学校筹款日的一个投注应用程序。它允许您在当天某些事件的结果上下注,例如哪支球队将赢得泡泡足球,或谁将赢得教师与学生的辩论,等等。我需要某处存储许多事件,其结果,事件和结果是单独的对象。我被告知存储这些的最佳方式是二进制。为此,我被告知要创建另一个应用程序,这将允许我创建,编辑和删除这些对象,然后将它们序列化为二进制文件以便稍后由应用程序反序列化。序列化为二进制工作。在应用程序方面进行反序列化,而不是那么多。

Heres the Serializer:

namespace MazBrokes_Editor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        {
        private List<Event> mEvents;
        private List<string> mEventNames = new List<string>();

        private Dictionary<Event, Outcome> mOutcomesToRemove = null;

        private List<string> mOutcomeNames;
        private static string file;
        private Event SelectedEvent = null;
        private Outcome SelectedOutcome = null;

        public MainWindow()
        {
            InitializeComponent();
        }


        private void btnEventAdd_Click(object sender, RoutedEventArgs e)
        {
            mEvents.Add(new Event(
                    txtEventName.Text,
                    txtEventType.Text,
                    txtEventDesc.Text,
                    new List<Outcome>()
                ));

        }

        private void btnAddOutcome_Click(object sender, RoutedEventArgs e)
        {
            if (txtEventOrigin.Text == "")
            {

              SelectedEvent.Outcomes.Add(new Outcome(txtName.Text, txtProbability.Text, txtPrizeDesc.Text, txtBetMultiplier.Text));

            }

            foreach (var item in mEvents)
            {

                if (item.eventName != txtEventOrigin.Text)
                {
                    continue;
                }

                else
                {
                    item.Outcomes.Add(new Outcome(txtName.Text, txtProbability.Text, txtPrizeDesc.Text, txtBetMultiplier.Text));
                }
            }
            Clear();
        }

        private void btnSerialize_Click(object sender, RoutedEventArgs e)
        {


            using (Stream stream = File.Open(file, FileMode.Create))
            {
                var binaryformatter = new BinaryFormatter();

                binaryformatter.Serialize(stream, mEvents);
            }

        }


            private void btnDeserialize_Click(object sender, RoutedEventArgs e)
            {
                FileStream fs = File.Open(file, FileMode.Open);

                BinaryFormatter formatter = new BinaryFormatter();


                mEvents = formatter.Deserialize(fs) as List<Event>;

                foreach(var item in mEvents)
                {
                    mEventNames.Add(item.eventName);
                }

                fs.Flush();
                fs.Close();
                fs.Dispose();

                lstDesirializedObjects.ItemsSource = mEventNames;

            }

            private void lstDesirializedObjects_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                    mOutcomeNames = new List<string>();

                   foreach (var item in mEvents)
                   {
                        if (lstDesirializedObjects.SelectedItem.ToString() != item.eventName)
                        {
                            continue;
                        }
                        else
                        {
                            txtEventName.Text = item.eventName;
                            txtEventDesc.Text = item.Description;
                            txtEventType.Text = item.eventType;
                            SelectedEvent = item;
                                foreach(var outcome in SelectedEvent.Outcomes)
                                {
                                    mOutcomeNames.Add(outcome.Name);

                                }
                                lstDesirializedOutcomes.ItemsSource = mOutcomeNames;

                        }
                   }

            }

        private void BtnEventEdit_Click(object sender, RoutedEventArgs e)
        {
            SelectedEvent.eventName = txtEventName.Text;
            SelectedEvent.Description = txtEventDesc.Text;
            SelectedEvent.eventType = txtEventType.Text;


        }

        private void lstDesirializedOutcomes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            foreach (var item in SelectedEvent.Outcomes)
            {
                if (lstDesirializedOutcomes.SelectedItem.ToString() != item.Name)
                {
                    continue;
                }
                else
                {

                    txtName.Text = item.Name;
                    txtProbability.Text = item.oddsArray[0] + "/" + item.oddsArray[1];
                    txtPrizeDesc.Text = item.Prize;
                    txtBetMultiplier.Text = Convert.ToString(item.mPayout);
                    SelectedOutcome = item;

                }
            }
        }

        private void btnEditOutcome_Click(object sender, RoutedEventArgs e)
        {
            SelectedOutcome.Name = txtName.Text;
            SelectedOutcome.oddsArray = txtProbability.Text.Split('/');
            SelectedOutcome.Prize = txtPrizeDesc.Text;
            SelectedOutcome.mPayout = Convert.ToDouble(txtBetMultiplier.Text);

            Clear();

        }

        private void btnDeleteOutcome_Click(object sender, RoutedEventArgs e)
        {
            mOutcomesToRemove = new Dictionary<Event, Outcome>();
            foreach (var Event in mEvents)
            {
                foreach (var outcome in Event.Outcomes)
                {
                    if (SelectedOutcome != outcome)
                    {
                        continue;
                    }
                    else
                    {
                        //Event.Outcomes.Remove(outcome);
                        mOutcomesToRemove.Add(Event, outcome);
                    }
                }
            }
            foreach (var item in mOutcomesToRemove)
            {
                item.Key.Outcomes.Remove(item.Value);
            }
        }

        private void DeleteOutcome(Event evvent, Outcome outcome)
        {
            evvent.Outcomes.Remove(outcome);
        }

        private void btnDeleteevent_Click(object sender, RoutedEventArgs e)
        {
            mEvents.Remove(SelectedEvent);
        }

        public void Clear()
        {
            txtEventOrigin.Text = "";
            txtName.Text = "";
            txtProbability.Text = "";
            txtPrizeDesc.Text = "";
            txtBetMultiplier.Text = "";
        }
    }
}`

解串器:

namespace MazBrokes
{
    class configParser
    {

        public static void SerializeParse()
        {

            BinaryFormatter formatter = new BinaryFormatter();
            SlidingTabFragment.UpcomingEvents.mEvents = formatter.Deserialize(MainActivity.assets.Open("MazBrokesData.bin")) as List<Events>;

        }


    }

}

我被抛出了这里详述的异常: BinaryFormatter deserialize gives SerializationException

我尝试了推荐的修补程序,但这只会导致更多错误。

您可以提供的任何帮助都非常受欢迎,如果我可以提供更多信息,请说。

1 个答案:

答案 0 :(得分:0)

事实证明,答案是完全废弃使用二进制文件。评论者Jason告诉我关于JSON的事情,我保留了编辑器程序,但不是序列化为二进制文件,而是序列化为JSON,主应用程序能够正确地解释它。