Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class FeePlanGenerate
 {
     public List<FeePlan> FeePlanList { get; set; }
     public List<FeeComponent> FeeComponentList { get; set; }

     public string StudentName { get; set;}
     public string StudentAddress { get; set; }
     public string CurriculumName { get; set; }
     public string CourseName { get; set; }
     public string BCNumber { get; set; }
     public Nullable< DateTime> AddmissionDate { get; set; }
     public string AddmissionOfficer{ get; set; }
     public string Contant1 { get; set; }
     public string Contant2 { get; set; }
     public string AdmissionCode { get; set; }
     public Nullable< decimal> TotalInvoiceAmount { get; set; }
     public Nullable<decimal> TotalPayableFee { get; set; }
     public string ArrayOfFeePlanGenerate { get; set; }
 }


What I have tried:

static string ConvertObjectToXMLString(object classObject)
        {
            string xmlString = null;
            XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
            using (MemoryStream memoryStream = new MemoryStream())
            {
                xmlSerializer.Serialize(memoryStream, classObject);
                memoryStream.Position = 0;
                xmlString = new StreamReader(memoryStream).ReadToEnd();
            }
            return xmlString;
        }
        public static FeePlanGenerate LoadFromXMLString(string xmlText)
        {
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "user";
            // xRoot.Namespace = "http://www.cpandl.com";
            xRoot.IsNullable = true;
            var stringReader = new System.IO.StringReader(xmlText);
            var serializer = new XmlSerializer(typeof(FeePlanGenerate),xRoot);
            return serializer.Deserialize(stringReader) as FeePlanGenerate;
        }
Posted
Updated 5-Nov-17 11:14am
Comments
Patrice T 5-Nov-17 14:59pm    
Which error message?
Graeme_Grant 5-Nov-17 20:24pm    
The one in the title of the question... ;)
Patrice T 5-Nov-17 20:30pm    
Probably :)
Graeme_Grant 5-Nov-17 21:01pm    
"XML to list it gives an error" ... If it is an XML array, then the error is obvious and answered below.

1 solution

The above code supplied is missing a couple of classes so I can't fully check where the error may be or what type of error that you are encountering. However, I used my own converter code and everything converted without an error.

Here is what I used:
C#
public static class XmlConverter
{
    public static string FromClass<T>(T data)
    {
        string response = string.Empty;

        var ms = new MemoryStream();
        try
        {
            ms = FromClassToStream(data);

            if (ms != null)
            {
                ms.Position = 0;
                using (var sr = new StreamReader(ms))
                    response = sr.ReadToEnd();
            }

        }
        finally
        {
            // don't want memory leaks...
            ms.Flush();
            ms.Dispose();
            ms = null;
        }

        return response;
    }

    public static MemoryStream FromClassToStream<T>(T data, XmlSerializerNamespaces ns = null)
    {
        var stream = default(MemoryStream);

        if (data != null)
        {
            var settings = new XmlWriterSettings()
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                ConformanceLevel = ConformanceLevel.Auto,
                CheckCharacters = true,
                OmitXmlDeclaration = false
            };

            try
            {
                XmlSerializer serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));

                stream = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    serializer.Serialize(writer, data, ns);
                    writer.Flush();
                }
                stream.Position = 0;
            }
            catch (Exception)
            {
                stream = default(MemoryStream);
            }
        }
        return stream;
    }

    public static T ToClass<T>(string data)
    {
        var response = default(T);

        if (!string.IsNullOrEmpty(data))
        {
            var settings = new XmlReaderSettings()
            {
                IgnoreWhitespace = true,
                DtdProcessing = DtdProcessing.Ignore
            };

            XmlSerializer serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));

            using (var sr = new StringReader(data))
            using (var reader = XmlReader.Create(sr, settings))
                response = (T)Convert.ChangeType(serializer.Deserialize(reader), typeof(T));
        }
        return response;
    }
}

// ref: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor/39642834#39642834
public static class XmlSerializerFactoryNoThrow
{
    public static Dictionary<Type, XmlSerializer> cache = new Dictionary<Type, XmlSerializer>();

    private static readonly object SyncRootCache = new object();

    public static XmlSerializer Create(Type type)
    {
        XmlSerializer serializer;

        lock (SyncRootCache)
            if (cache.TryGetValue(type, out serializer))
                return serializer;

        //multiple variable of type of one type is same instance
        lock (type)
        {
            //constructor XmlSerializer.FromTypes does not throw the first chance exception           
            serializer = XmlSerializer.FromTypes(new[] { type })[0];
        }

        lock (SyncRootCache) cache[type] = serializer;
        return serializer;
    }
}

And here is how to use it:
C#
var data = new FeePlanGenerate();
var xmlString = XmlConverter.FromClass(data);

UPDATE I just re-read your question title and saw that the problem was in fact with the List properties in the class provided.

The answer is dependant on how the XML data is formatted. My guess is that the nodes are one beneath the other but without seeing the raw XML that you want to deserialize it is difficult to say.

My best guess would be to do the following:
C#
public class FeePlanGenerate
{
    [XmlElement]
    public List<FeePlan> FeePlanList { get; set; }

    [XmlElement]
    public List<FeeComponent> FeeComponentList { get; set; }

    public string StudentName { get; set;}
    public string StudentAddress { get; set; }
    public string CurriculumName { get; set; }
    public string CourseName { get; set; }
    public string BCNumber { get; set; }
    public DateTime? AddmissionDate { get; set; }
    public string AddmissionOfficer{ get; set; }
    public string Contant1 { get; set; }
    public string Contant2 { get; set; }
    public string AdmissionCode { get; set; }
    public decimal? TotalInvoiceAmount { get; set; }
    public decimal? TotalPayableFee { get; set; }
    public string ArrayOfFeePlanGenerate { get; set; }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900