locked
Issue with my xml method, back slashes and xml delaration issue RRS feed

  • Question

  • User1034446946 posted

    Hi

    I have this method

     private static XmlDocument CreateSoapEnvelope<T>(ShippingEnvolopeHeaderModel header, T model)
            {
                //Base envolope
                var env = @"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""><SOAP-ENV:Header><soapenv:Header><wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext1.0.xsd"" xmlns:wsu=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""><wsse:UsernameToken wsu:Id=""{0}""><wsse:Username>{1}</wsse:Username><wsse:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-tokenprofile-1.0#PasswordDigest"">{2}</wsse:Password><wsse:Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soapmessage-security-1.0#Base64Binary"">{3}</wsse:Nonce><wsu:Created>{4}</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header></SOAP-ENV:Header><SOAP-ENV:Body>{5}</SOAP-ENV:Body></SOAP-ENV:Envelope>";
    
                //Convert the model to xml string
                var modelString = XMLUtilities.Serialize(model);
    
                //format the envolope with the required values
                var xmlString = string.Format(env, header.UsernameToken, header.Username, header.Password, header.Nonce, header.Created, modelString);
    
                //clean xml string 
                xmlString.Replace("\","");
                //Create XMLDocument
                XmlDocument soapEnvelopeDocument = new XmlDocument();
                soapEnvelopeDocument.LoadXml(xmlString);
    
                return soapEnvelopeDocument;
            }



     public static  string Serialize<T>(T ObjectToSerialize)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
    
                using (StringWriter textWriter = new StringWriter())
                {
                    xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                    return textWriter.ToString();
                }
            }

    and i have 2 issues env ends up with backslashes in it at the front and end of all the uri (i think they are uri's)

    and

    when i run modelString its add the xml declaration at the start <xml> and i don't want that I just want the models as directed.

    any help would be appriciated.

    Wednesday, May 23, 2018 3:17 PM

Answers