none
How to add a prefix to a namespace in an existing XML RRS feed

  • Question

  • Hi

    I am implementing a client using WCF. The web service to be consumed is exposed by another company and can't be changed.. It is expecting a very specific XML string as "application/soap+xml" input and returns some "text/html" output (Don't ask why input and output is not same content type :/).

    So... I had to use MessageContract and a custom MessageEncoder to both generate the expected input and to be able to understand the answer.

    On my MessageContract, I declared the service to be consumed as

    [XmlSerializerFormatAttribute()]
    [ServiceContract(Namespace="http://xxx/B2B", ConfigurationName="...")]
    public interface IService1 {}
    

    In my custom MessageEncoder, I have among other to add a prefix to each XmlNode, which is very simple (xmlNode.Prefix = "xxx";). But doing this, in addition to the prefix, I also get two namespaces defined, whose one which is defined with my prefix and the other one which is the original one due to the attribute ServiceContract.

    The original xml issued from the default Xml Serialization looks like this:

    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Body>
    <B2BRegistration xmlns="http://xxxx/B2B">
    </B2BRegistration>
    </env:Body>
    </env:Envelope>

     Once I have added the prefix on the nodes, I get the following output:

    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Body>
    <xxx:B2BRegistration xmlns="http://xxxx/B2B" xmlns:xxx="http://xxxx/B2B">
    </xxx:B2BRegistration>
    </env:Body>
    </env:Envelope>

    In this Xml, you can see both xmlns="http://xxxx/B2B" and xmlns:xxx="http://xxxx/B2B". The first one should not be there (as not expected by the web services).

    Could someone help me to find how to either prefix the namespace issued from the attribute ServiceContract or to remove this namespace as another one is added by my manipulation. I did try to manipulate the Xml (in a XmlDocument) with a XmlNamespaceManager but didn't succeed.

    Thanks a lot in advance for any tip !

    V.

     

     

    • Moved by John SaundersModerator Tuesday, May 11, 2010 2:47 PM WCF Question (From:ASMX Web Services and XML Serialization)
    Tuesday, May 11, 2010 8:45 AM

Answers

  • Hi,

    Let me try to simplify the question. Your question is, given xml:

    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Body>
    <B2BRegistration xmlns="http://xxxx/B2B">
    </B2BRegistration>
    </env:Body>
    </env:Envelope>

    You want to get the following xml:

    <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
    <env:Body>
    <xxx:B2BRegistration xmlns:xxx="http://xxxx/B2B">
    </xxx:B2BRegistration>
    </env:Body>
    </env:Envelope>

    Right? If so you can try the following code:

     static void Main(string[] args)
            {
                var originalXML = @"<env:Envelope xmlns:env=""http://www.w3.org/2003/05/soap-envelope"">
    <env:Body>
    <B2BRegistration xmlns=""http://xxxx/B2B"">
    </B2BRegistration>
    </env:Body>
    </env:Envelope>";
                XDocument doc = XDocument.Parse(originalXML);
                XNamespace ns = "http://xxxx/B2B";
               var element= doc.Descendants(ns+"B2BRegistration").FirstOrDefault();
               element.Attributes("xmlns").FirstOrDefault().Remove();
                element.Add(new XAttribute(XNamespace.Xmlns + "xxx", "http://xxxx/B2B"));
                Console.WriteLine(doc);
                Console.ReadLine();
            }

     You may also refer to the following documentation to learn how to modify the soap message before sending to server:

    http://msdn.microsoft.com/en-us/library/aa717047.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Windows Azure Platform China Blog: http://blogs.msdn.com/azchina/default.aspx
    Thursday, May 13, 2010 3:24 AM
    Moderator