none
Token StartElement in state Epilog would result in an invalid XML document with Soap Serializer

    Question

  • Hi, I need to serialize class into soap xml stream.

    When I do xmlSerializer.Serialize I could see following error "Token StartElement in state Epilog would result in an invalid XML document with Soap Serializer"

    Here Is just a few simple code in order to reproduce this behaviour.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApp1
    {
        [SoapType(TypeName = "Child")]
        public class Child
        {
            public String ChildTag1 { get; set; }
        }
    }



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApp1
    {
        [SoapType(TypeName = "Master"), SoapInclude(typeof(Child))]
        public class Master
        {
            public String MasterTag1 { get; set; }
            public Child[] Childs { get; set; }
        }
    }

    and finally xml soap serialization:

    using System.IO;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Master mas = new Master();
                mas.MasterTag1 = "Tag1";
                Child ch1 = new Child();
                ch1.ChildTag1 = "Child1Tag1";
                Child ch2 = new Child();
                ch2.ChildTag1 = "Child2Tag1";
                mas.Childs = new Child[] { ch1, ch2 };
    
                MemoryStream target = new MemoryStream();
                System.Xml.Serialization.XmlTypeMapping xmlTypeMapping = (new System.Xml.Serialization.SoapReflectionImporter().ImportTypeMapping(mas.GetType()));
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(xmlTypeMapping);
                xmlSerializer.Serialize(target, mas);
                byte[] result = target.ToArray();
            }
        }
    }

    When I comment out this line:  public Child[] Childs { get; set; } on master.cs class all works fine!

    Could anyone help me to solve this issue?

    Thanks


    FabioBo




    • Edited by FabioBo Saturday, October 12, 2013 5:01 PM
    Saturday, October 12, 2013 4:58 PM

Answers

  • Hi Fred,

    thank you for your quick response.

    I've tried the solution you have suggested but does not work for me.

    I need to get the result back into base64Binary field of Web Servce.

    I've tried an alternative solution, that's works fine:

                MemoryStream target = new MemoryStream();
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(mas.GetType());
                xmlSerializer.Serialize(target, mas);
                byte[] result = target.ToArray();
    
    Thank you


    FabioBo

    • Marked as answer by FabioBo Monday, October 14, 2013 5:05 PM
    Monday, October 14, 2013 2:44 PM

All replies

  • Hello,

    From your description, you want to serialize object to XML using SOAP. With your codes, I made a sample and please have a look at it below:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Xml.Serialization;
    
    
    namespace XMLSerialize
    
    {
    
        [Serializable]
    
        public class Master
    
        {
    
            public String MasterID { get; set; }
    
            
    
            public Child[] Childs { get; set; }
    
        }
    
    
    }
    
    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Xml.Serialization;
    
    
    namespace XMLSerialize
    
    {
    
        [Serializable]
    
        public class Child
    
        {
    
            public String ChildID { get; set; }
    
        }
    
    
    }
    
    using System;
    
    using System.Collections.Generic;
    
    using System.IO;
    
    using System.Linq;
    
    using System.Runtime.Serialization.Formatters.Soap;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Xml;
    
    
    namespace XMLSerialize
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                string filePath = "E:\\BMX\\Lab\\SmapleFile\\2013-10\\Sample_14.xml";
    
    
                Master mas = new Master();
    
                mas.MasterID = "MasterID";
    
                Child ch1 = new Child();
    
                ch1.ChildID = "ChildID";
    
                Child ch2 = new Child();
    
                ch2.ChildID = "ChildID";
    
                mas.Childs = new Child[] { ch1, ch2 };
    
    
                FileStream fileStream = null;
    
                try
    
                {
    
                    fileStream = new FileStream(filePath,
    
                           FileMode.Create, FileAccess.Write);
    
                    SoapFormatter formatter = new SoapFormatter();
    
                    formatter.Serialize(fileStream, mas);
    
                }
    
                catch (Exception exception)
    
                {
    
                    throw exception;
    
                }
    
                finally
    
                {
    
                    if (fileStream != null) fileStream.Close();
    
                }
    
    
            }
    
    
        }
    
    }

    The result:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 14, 2013 7:26 AM
    Moderator
  • Hi Fred,

    thank you for your quick response.

    I've tried the solution you have suggested but does not work for me.

    I need to get the result back into base64Binary field of Web Servce.

    I've tried an alternative solution, that's works fine:

                MemoryStream target = new MemoryStream();
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(mas.GetType());
                xmlSerializer.Serialize(target, mas);
                byte[] result = target.ToArray();
    
    Thank you


    FabioBo

    • Marked as answer by FabioBo Monday, October 14, 2013 5:05 PM
    Monday, October 14, 2013 2:44 PM
  • Glad to hear that you have found a solution for it.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 15, 2013 3:45 AM
    Moderator