Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

xml serialization - XmlSerializer Giving a Null exception in c#

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This doesn't look right:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...