C# Serialize object to SOAP string array issue

徘徊边缘 提交于 2019-12-22 13:00:49

问题


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;
    }

来源:https://stackoverflow.com/questions/52893279/c-sharp-serialize-object-to-soap-string-array-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!