本文介绍了InvalidOperationException异常而复杂类型SOAP序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到SOAP序列化的问题,这将是巨大的,找到答案。这里有一个很简单的例子:

I encountered a problem with SOAP serialization and it would be great to find an answer. Here's a very simplified example:

public void Test()
{
    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb);

    SoapReflectionImporter importer = new SoapReflectionImporter();
    XmlTypeMapping map = importer.ImportTypeMapping(typeof(A));
    XmlSerializer serializer = new XmlSerializer(map);
    serializer.Serialize(writer, new A());
}

[Serializable]
public class A
{
    public A()
    {
        BB = new B();
    }

    public int a;

    public B BB;
}
[Serializable]
public class B
{
    public int A1 { get; set; }

    public int A2 { get; set; }
}

如果我运行的方法测试(),然后我得到以下异常: System.InvalidOperationException:令牌的StartElement状态结语将导致一个无效的XML文档中

If I run method Test() then I get the following exception: System.InvalidOperationException: Token StartElement in state Epilog would result in an invalid XML document.

请问AP preciate任何帮助。

Would appreciate any help.

推荐答案

使用的XmlWriter代替的StringWriter并做writer.WriteStartElement(根);

Use XmlWriter instead of StringWriter and do a writer.WriteStartElement("root");

这将工作:

Stream s = new MemoryStream();
XmlWriter writer = new XmlTextWriter(s, Encoding.UTF8);

SoapReflectionImporter importer = new SoapReflectionImporter();
XmlTypeMapping map = importer.ImportTypeMapping(typeof(A));
XmlSerializer serializer = new XmlSerializer(map);
writer.WriteStartElement("root");
serializer.Serialize(writer, new A());

StreamReader sr = new StreamReader(s);
string data = sr.ReadToEnd();

这篇关于InvalidOperationException异常而复杂类型SOAP序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:40