我有一些从 XML Serializer 生成的 xml ..How can I convert it to SOAP XML ?...我正在尝试这样做 ASP.NET C#...请帮帮我

最佳答案

您只需要创建一个可以被 XMLSerializer 和 SOAPFormatter 序列化的数据类。这可能意味着您将需要一个具有 XMLSerializer 公共(public)属性的公共(public)类,并且您将需要为 SOAPFormatter 添加 Serializable 属性。否则,它是非常简单的。

我创建了一个 Naive 示例来说明我的意思:

[Serializable]
public class MyData
{
    public int MyNumber { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            MyData data = new MyData() { MyNumber = 11, Name = "StackOverflow" };

            XmlSerializer serializerXML = new XmlSerializer(data.GetType());
            serializerXML.Serialize(stream, data);

            stream.Seek(0, SeekOrigin.Begin);

            data = (MyData)serializerXML.Deserialize(stream);

            // We're cheating here, because I assume the SOAP data
            // will be larger than the previous stream.
            stream.Seek(0, SeekOrigin.Begin);

            SoapFormatter serializerSoap = new SoapFormatter();
            serializerSoap.Serialize(stream, data);

            stream.Seek(0, SeekOrigin.Begin);

            data = (MyData)serializerSoap.Deserialize(stream);
        }
    }
}

关于c# - 如何在 C# 中将原始 XML 转换为 SOAP XML?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3256461/

10-13 03:24