XmlSerializer Giving a Null exception in c#

吃可爱长大的小学妹 提交于 2019-12-13 10:42:12

问题


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

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

Here is my class:

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.


回答1:


This doesn't look right:

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



回答2:


At first check the node is exist,for exist write down the code

XmlNode xmlNode = voucharbrandnode.SelectSingleNode("node name");

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


来源:https://stackoverflow.com/questions/14836017/xmlserializer-giving-a-null-exception-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!