[英]How to serialize to XML / SOAP an object with property that is another complex object type? I keep getting invalid document structure
我正在尝试将一组非常简单的 C# 代码序列化为 XML / SOAP。 我试图序列化的 class 类型有一个属性,它是另一个 class 类型。 我不断收到一个错误,即文档在 Epilog 中失败,并会创建一个无效的 XML 文档。 确切的错误消息如下...
state Epilog 中的令牌 StartElement 将导致 XML 文档无效。
下面是我的测试代码集...
using System;
using System.IO;
using System.Xml.Serialization;
public class Program
{
public static void Main()
{
var mapping = new SoapReflectionImporter().ImportTypeMapping(typeof(Envelope));
var serializer = new XmlSerializer(mapping);
var envelope = new Envelope
{
Body = new Body
{
Foo = "Testing"
}
};
using (var destination = new StringWriter())
{
serializer.Serialize(destination, envelope);
var result = destination.ToString();
Console.WriteLine(result);
}
}
public class Envelope
{
public Body Body { get; set; }
}
public class Body
{
public string Foo { get; set; }
}
}
这是我指向 .net 小提琴页面的链接: https://dotnetfiddle.net/u0oyUo
我怎样才能让这个序列化呢?
你为什么使用:
var mapping = new SoapReflectionImporter().ImportTypeMapping(typeof(Envelope));
有没有具体的原因? 如果您跳过该部分并将以下行更改为:
var serializer = new XmlSerializer(typeof(Envelope));
它将 class 序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.