How to convert Raw XML to SOAP XML in C #?

I have xml generated from an XML serializer. How can I convert it to SOAP XML? ... I'm trying to do this ASP.NET C # ... please help me

+3
source share
3 answers

You just need to create a data class that can be serialized by both XMLSerializer and SOAPFormatter. This probably means that for XMLSerializer you will need an open class with public properties, and you will need to add the Serializable attribute for the SOAPFormatter. Otherwise, it is pretty straight forward.

I created an Naive example to illustrate what I mean:

[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);
        }
    }
}
+1
source

, "raw XML" "SOAP XML".

? XML -, XmlDocument XDocument :

[WebMethod]
public XmlElement ReturnXml()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(fromSomewhere);
    return doc.DocumentElement;
}
+1

Does it look like you want to turn your xml into a soap envelope? if yes try this

0
source

Source: https://habr.com/ru/post/1754823/


All Articles