locked
C# XML Element Prefix SOAP RRS feed

  • Question

  • User1358036820 posted

    I have seen many SO answers but non seem to help with the issue I have. What I am doing is calling a SOAP service from third party and it actually requires a prefix on element names. Example:

    <pref:test>test</pref:test>

    I have created a client using Add service reference in VS. I have a class which is where i want the prefix on each property.

    public class WebsiteSoapHeader : System.Web.Services.Protocols.SoapHeader
    	{
    		public WebsiteSoapHeader()
    		{
    			xmlns.Add("test", "https://xxx/service");
    		}
    
    		[XmlNamespaceDeclarations]
    		public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
    		[XmlElement(ElementName ="test",Namespace = "https://xxx/service")]
    		public string testprop { get; set; }
    	}

    As this does not get serialized manually it only produces

    <test xmlns="">test</test>

    Monday, March 27, 2017 7:15 AM

All replies

  • User-2057865890 posted

    Hi Ammd,

    Take a look the following article.

    #Changing prefixes in XML responses for WCF services
    https://blogs.msdn.microsoft.com/carlosfigueira/2010/06/12/changing-prefixes-in-xml-responses-for-wcf-services/ 

    Best Regards,

    Chris

    Tuesday, March 28, 2017 6:48 AM
  • User1358036820 posted

    Hi Chris

    I saw this but mine is a auto generated proxy from wsdl and i don't have those methods to work with.  At this point i'm using a manual approach with stringbuilder but not very clean.

    Tuesday, March 28, 2017 9:56 PM
  • User1168443798 posted

    >> I saw this but mine is a auto generated proxy from wsdl and i don't have those methods to work with

    How did you use the generated proxy? I suggest you replace the binding in client side. For my above link, it set below code for service, you could also set it for client binding.

    Binding binding = ReplacePrefixMessageEncodingBindingElement.ReplaceEncodingBindingElement( 
                new WSHttpBinding(SecurityMode.None), 
                namespaceToPrefixMapping);
    

    In addition, the link below shares another way to custom the Prefix.

    # Customize WCF Envelope and Namespace Prefix

    http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix

    Friday, March 31, 2017 6:56 AM