Microsoft.Protocols.TestTools.StackSdk.WsDiscovery.SoapMessage.SoapEnvelope.Serialize C# (CSharp) Method

Serialize() public static method

Serialize the soap message
public static Serialize ( SoapEnvelope envelope ) : string
envelope SoapEnvelope The soap envelope
return string
        public static string Serialize(SoapEnvelope envelope)
        {
            string result = null;

            XmlAttributes attrs = new XmlAttributes();

            XmlElementAttribute attr = new XmlElementAttribute();
            attr.ElementName = "Body";
            attr.Type = envelope.body.GetType();
            attrs.XmlElements.Add(attr);

            XmlAttributes attrs2 = new XmlAttributes();
            XmlElementAttribute attr2 = new XmlElementAttribute();
            attr2.ElementName = "Header";
            attr2.Type = envelope.header.GetType();

            attrs2.XmlElements.Add(attr2);

            XmlAttributeOverrides attros = new XmlAttributeOverrides();
            attros.Add(typeof(SoapEnvelope), "Body", attrs);
            attros.Add(typeof(SoapEnvelope), "Header", attrs2);

            XmlSerializer a = new XmlSerializer(typeof(SoapEnvelope), attros);

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("soap", "http://www.w3.org/2003/05/soap-envelope");
            ns.Add("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
            ns.Add("wsd", "http://schemas.xmlsoap.org/ws/2005/04/discovery");
            ns.Add("PeerDist", "http://schemas.microsoft.com/p2p/2007/09/PeerDistributionDiscovery");

            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
                writer.Formatting = Formatting.None;
                a.Serialize(writer, envelope, ns);
                writer.Close();
                result = Encoding.UTF8.GetString(ms.ToArray());
            }

            return result;
        }