簡體   English   中英

如何序列化到 XML / SOAP 和 object 具有另一個復雜的 ZA8CFDE6331BD59EB66AC96F8911 類型的屬性我不斷收到無效的文檔結構

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM