XmlSerializer 在 C# 中给出 Null 异常 [英] XmlSerializer Giving a Null exception in c#

查看:67
本文介绍了XmlSerializer 在 C# 中给出 Null 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向能够告诉我为什么这不起作用的人的金牌.仅供参考,它没有内部异常文本,所以它根本没有帮助我......

A gold medal to the person who can tell me why this isnt working. FYI it has no inner exception text so its not helping me at all...

XmlSerializer x = new XmlSerializer(typeof(DownloadedSite));

我的内在异常只是 Null.我的错误是:{未将对象引用设置为对象的实例."}

My innerexception is just Null. My error is: {"Object reference not set to an instance of an object."}

这是我的课:

namespace WayBackMachine.Business.Obj
{
    using System;
    using System.Xml.Serialization;
    using System.Linq;
    using System.Collections.Generic;

    [Serializable]
    [XmlRoot("DownloadedSite")]
    public class DownloadedSite : ManagedObject
    {
        private SortableBindingList<SiteSource> sources;// = new SortableBindingList<SiteSource>();
        private SortableBindingList<SiteSource> links;// = new SortableBindingList<SiteSource>();
        private SortableBindingList<Page> pages;// = new SortableBindingList<Page>();
        private string name;
        private string defaultSite;

        public DownloadedSite() : base()
        {
            sources = new SortableBindingList<SiteSource>();
            links = new SortableBindingList<SiteSource>();
            pages = new SortableBindingList<Page>();
            name = "";
            defaultSite = "";
        }

        [XmlArray("Sources")]
        [XmlArrayItem("SiteSource", typeof(SiteSource))]
        public SortableBindingList<SiteSource> Sources
        {
            get { return this.sources; }
            set
            {
                this.CheckPropertyChanged<SortableBindingList<SiteSource>>
                ("Sources", ref this.sources, ref value);
            }
        }

        [XmlArray("Links")]
        [XmlArrayItem("SiteSource", typeof(SiteSource))]
        public SortableBindingList<SiteSource> Links
        {
            get { return this.links; }
            set
            {
                this.CheckPropertyChanged<SortableBindingList<SiteSource>>
                ("Links", ref this.links, ref value);
            }
        }

        [XmlArray("Pages")]
        [XmlArrayItem("Page", typeof(SiteSource))]
        public SortableBindingList<Page> Pages
        {
            get { return this.pages; }
            set
            {
                this.CheckPropertyChanged<SortableBindingList<Page>>
                ("Pages", ref this.pages, ref value);
            }
        }

        [XmlElement("SiteName")]
        public string Name
        {
            get { return this.name; }
            set
            {
                CheckPropertyChanged<string>
                ("Name", ref this.name, ref value);
            }
        }

        [XmlElement("DefaultSite")]
        public string DefaultSite
        {
            get { return this.defaultSite; }
            set
            {
                CheckPropertyChanged<string>
                ("DefaultSite", ref this.defaultSite, ref value);
            }
        }




    }
}

如果有帮助,这里是它继承的另一个类:

If it helps, here is the other class it inherits from:

namespace WayBackMachine.Business
{
    using System.ComponentModel;
    using System;
    using System.Xml.Serialization;

    [Serializable]
    [XmlRoot("ManagedObject")]
    public class ManagedObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, ref T newValue)
        {
            if (oldValue == null && newValue == null)
            {
                return false;
            }
            if (oldValue == null && newValue != null || !oldValue.Equals((T)newValue))
            {
                oldValue = newValue;
                FirePropertyChanged(propertyName);
                return true;
            }
            return false;
        }

        protected void FirePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

有没有人知道,这让我发疯.

Does anyone have any idea, its driving me mad.

推荐答案

这看起来不对:

 [XmlArray("Pages")]
 [XmlArrayItem("Page", typeof(SiteSource))] <-- should be typeof(Page)?
 public SortableBindingList<Page> Pages

这篇关于XmlSerializer 在 C# 中给出 Null 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆