2

I have an issue where trying to serialize an object containing a string array to soap causes an exception in my application. I am doing the following to create the soap formatter:

XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType());
        XmlSerializer serializer = new XmlSerializer(mapping);

when I call Serialize on the serializer I get the following exception. "Token StartElement in state Epilog would result in an invalid XML document."

However if I just want regular xml and create my XmlSerializer like this:

XmlSerializer serializer = new XmlSerializer(obj.GetType());

Everything works fine and the xml contains the string array.

I have a full example below that reproduces the issue on my machine if someone could take a look I would be very grateful as I am out of ideas!

 static void Main(string[] args)
    {
        GetAlarmEventTypesResponse bob = new GetAlarmEventTypesResponse();
        bob.GetAlarmEventTypesTypes = new string[] { "bob", "bob1", "bob2" };
        bob.version = "2.0";


        // works
        string xml = GetRegularDocument(bob);
        Console.WriteLine(xml);

        // throws exception
        string soap = GetSoapDocument(bob);
        Console.WriteLine(soap);
    }

    //------------------------------------------------------------------------------

    [System.Xml.Serialization.SoapTypeAttribute(Namespace = "http://example/common/dataexchange/2011/05")]
    public class GetAlarmEventTypesResponse
    {
        public GetAlarmEventTypesResponse()
        {
            version = "1.2";
        }

        [System.Xml.Serialization.XmlArrayItemAttribute("Type", IsNullable = false)]
        public string[] GetAlarmEventTypesTypes { get; set; }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string version { get; set; }
    }

    //------------------------------------------------------------------------------

    public static string GetRegularDocument(object obj)
    {
        string document = null;

        XmlSerializer serializer = new XmlSerializer(obj.GetType());

        using (StringWriter textWriter = new StringWriter())
        {
            serializer.Serialize(textWriter, obj);
            document = textWriter.ToString();
        }
        return document;
    }

    //------------------------------------------------------------------------------

    public static string GetSoapDocument(object obj)
    {
        string document = null;

        XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType());
        XmlSerializer serializer = new XmlSerializer(mapping);

        using (StringWriter textWriter = new StringWriter())
        {
            serializer.Serialize(textWriter, obj);
            document = textWriter.ToString();
        }
        return document;
    }
  • This looks to be a duplicate of [Extension method to serialize generic objects as a SOAP formatted stream](https://stackoverflow.com/q/6401075). The basic issue is that the serializer generated by `SoapReflectionImporter` writes the SOAP body, leaving it to the caller to write the soap header root element. Since you didn't write the root element, you get an exception. If you just want to see the SOAP body as an XML fragment, you can use `SoapSerializationHelper` from this fiddle: https://dotnetfiddle.net/SZDNsY – dbc Oct 30 '18 at 23:28

0 Answers0