XmlSerializer在c#中给出Null异常

时间:2013-02-12 15:33:21

标签: c# xml-serialization nullreferenceexception

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

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

我的感觉只是空洞。我的错误是: {“对象引用未设置为对象的实例。”}

这是我的班级:

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);
            }
        }




    }
}

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

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));
            }
        }
    }
}

有没有人有任何想法,它让我发疯。

2 个答案:

答案 0 :(得分:1)

这看起来不对:

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

答案 1 :(得分:0)

首先检查节点是否存在,为存在写下代码

XmlNode xmlNode = voucharbrandnode.SelectSingleNode(&#34;节点名称&#34;);

if (xmlNode != null)
{
write some code
}