Why won't this XMLSerialize work?

64 views Asked by At

I am struggling to debug this because it is in a C# DLL and I don't know how to debug from the MFC executable that called it.

Now, if I call this method:

public void SavePublisherData(out Int64 iResult)
{
    using (System.IO.StreamWriter file =
                new System.IO.StreamWriter(_strPathXML, true))
    {
        file.WriteLine("This is a test");
    }

    iResult = MakeResult(true);
}

If I call that from my MFC application is works. I end up with a file. But if I do what I really want I get no results. I have this Publisher class:

using System.Xml.Serialization;

namespace MSAToolsLibrary
{
    class Publisher
    {
        public Publisher()
        {

        }

        [XmlElement]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private string _Name;

        [XmlElement]
        public string Notes
        {
            get { return _Notes; }
            set { _Notes = value; }
        }
        private string _Notes;
   }
 }

And I have a parent class:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;


namespace MSAToolsLibrary
{
    [XmlRoot(ElementName = "PublisherDatabase", Namespace = "http://www.publictalksoftware.co.uk/msa")]
    class PublisherData
    {
        public PublisherData()
        {
            _Publishers = new List<Publisher>();
        }

        [XmlArray]
        public List<Publisher> Publishers
        {
            get { return _Publishers; }
            set { _Publishers = value; }
        }
        private List<Publisher> _Publishers;

        public void AddStudent(String strName, String strNotes)
        {
            Publisher _Publisher = new Publisher()
            {
                Name = strName,
                Notes = strNotes
            };
            _Publishers.Add(_Publisher);
        }
    }
}

The DLL library performs the save like this:

public void SavePublisherData(out Int64 iResult)
{
    try
    {
        XmlSerializer x = new XmlSerializer(_PublisherData.GetType());
        using (StreamWriter writer = new StreamWriter("d:\\andrew-test-3.xml"))
        {
            x.Serialize(writer, _PublisherData);
        }
    }
    catch
    {
        iResult = MakeResult(false);
        return;
    }

    iResult = MakeResult(true);
}

The return result is true. I get no exceptions raised. But I get no file created.

I have added 4 items into the Publishers array so I am expecting it to have created a simple XML file.

What is my mistake?

1

There are 1 answers

1
Tsef On BEST ANSWER

XmlSerializer works with public classes only, both your PublisherData & Publisher classes are internal.