locked
Remove namespace from ASP.NET web service, or add namespace prefix? RRS feed

  • Question

  • We're writing web services in .Net.  Some of our client applications are written in Adobe Flex.  Evidently Flex has trouble reading data from a .Net web service if the xmlns attribute doesn't have a prefix.  For example, here's a simple web service method that returns a predefined class:

     

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel

     

    <System.Web.Services.WebService(Namespace:="http://mycompany.com/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class NamespaceTest
        Inherits System.Web.Services.WebService

     

        <WebMethod()> _
        Public Function HelloWorld() As WSObject
            Try
                Dim obj As New WSObject

     

                obj.Field1 = 123
                obj.Field2 = "Test"
                obj.Field3 = Now

     

                Return obj
            Catch ex As Exception
                Throw ex
            End Try
        End Function

     

        Public Class WSObject
            Public Field1 As Int32 = Nothing
            Public Field2 As String = ""
            Public Field3 As DateTime = Nothing
        End Class

     

    End Class

     

    Invoking this web service returns:

     

    <?xml version="1.0" encoding="utf-8"?>
    <WSObject xmlns:xsi="
    http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://mycompany.com/">
      <Field1>123</Field1>
      <Field2>Test</Field2>
      <Field3>2007-08-31T15:17:22.0061053-07:00</Field3>
    </WSObject>

     

    Is there any way to either eliminate the xmlns=http://mycompany.com/ attribute (get rid of it, not change the value)?

     

    Or, is there any way to add a prefix to it, such as:

     

    <?xml version="1.0" encoding="utf-8"?>
    <WSObject xmlns:xsi="
    http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://mycompany.com/">
      <ns:Field1>123</ns:Field1>
      <ns:Field2>Test</ns:Field2>
      <ns:Field3>2007-08-31T15:17:22.0061053-07:00</ns:Field3>
    </WSObject>

     

    Any help would be greatly appreciated.  It's very frustrating to think you'll have to jump through a bunch of hoops and not be able to use the simple features of ASP.NET web services because of a small problem like this.

     

    Thanks,

     

    Sean.

    Friday, August 31, 2007 10:25 PM

Answers

  • Two options come to mind:

     

    Set the namespace to blank in the WebService() attribute (and also remove your WebServiceBinding attribute)

     

    OR

     

    Use the XmlNamespaceDeclaration attribute to fully-qualify all references.

     

    Here's some code for both:

     

    Code Snippet

    #if NONAMESPACES

        [WebService(Namespace = "")]

    #else

        [WebService(Namespace = "http://mynamespace/")]

        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    #endif

        public class Service1 : System.Web.Services.WebService

        {

            public class Customer

            {

                [XmlNamespaceDeclarations]

                public XmlSerializerNamespaces xmlns

                {

                    get

                    {

                        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

                        xsn.Add("me", "http://mynamespace/");

                        return xsn;

                    }

                    set { /* needed for xml serialization */ }

                }

     

                public string name;

                public Customer Parent;

            }

     

            [WebMethod]

            public Customer GetCustomer()

            {

                Customer a = new Customer();

                a.name = "a";

                Customer b = new Customer();

                b.name = "b";

                a.Parent = b;

                return a;

            }

     

    Wednesday, September 5, 2007 7:55 PM
    Moderator
  • Either this software does not adhere to XML standards that have been in place for nearly a decade, or this is not actually the problem you're having.

     

    I don't know Adobe Flex, so I don't know if it was written by incompetent developers. You would be a better judge of that than I would. If you don't think they are all idiots, then you should assume that they did not make this mistake and try to figure out what else it could be.

     

    "When you have eliminated the impossible, that which remains, however unlikley, is the truth."

     

    S. Holmes

     

    "If you assume they're not idiots, then, unlikely as it sounds, this isn't the problem."

     

    J. Saunders

     

    Saturday, September 1, 2007 3:33 AM
    Moderator

All replies

  • Either this software does not adhere to XML standards that have been in place for nearly a decade, or this is not actually the problem you're having.

     

    I don't know Adobe Flex, so I don't know if it was written by incompetent developers. You would be a better judge of that than I would. If you don't think they are all idiots, then you should assume that they did not make this mistake and try to figure out what else it could be.

     

    "When you have eliminated the impossible, that which remains, however unlikley, is the truth."

     

    S. Holmes

     

    "If you assume they're not idiots, then, unlikely as it sounds, this isn't the problem."

     

    J. Saunders

     

    Saturday, September 1, 2007 3:33 AM
    Moderator
  • Of course you are correct.  I spent a couple hours this morning learning Flex and built a simple app that read my web service fine.  So, they're doing something in Flex that's screwing something up.  I'll need to spend some more time figuring out what that is.

     

    However, things being as they are, I may still be "forced" to remove the namespaces from the response.  If someone has a simple way to do that, I'd be very interested.

     

    Thanks,

     

    Sean.

     

     John Saunders wrote:

    Either this software does not adhere to XML standards that have been in place for nearly a decade, or this is not actually the problem you're having.

     

    I don't know Adobe Flex, so I don't know if it was written by incompetent developers. You would be a better judge of that than I would. If you don't think they are all idiots, then you should assume that they did not make this mistake and try to figure out what else it could be.

     

    "When you have eliminated the impossible, that which remains, however unlikley, is the truth."

     

    S. Holmes

     

    "If you assume they're not idiots, then, unlikely as it sounds, this isn't the problem."

     

    J. Saunders

     

    Tuesday, September 4, 2007 5:23 PM
  • Two options come to mind:

     

    Set the namespace to blank in the WebService() attribute (and also remove your WebServiceBinding attribute)

     

    OR

     

    Use the XmlNamespaceDeclaration attribute to fully-qualify all references.

     

    Here's some code for both:

     

    Code Snippet

    #if NONAMESPACES

        [WebService(Namespace = "")]

    #else

        [WebService(Namespace = "http://mynamespace/")]

        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    #endif

        public class Service1 : System.Web.Services.WebService

        {

            public class Customer

            {

                [XmlNamespaceDeclarations]

                public XmlSerializerNamespaces xmlns

                {

                    get

                    {

                        XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();

                        xsn.Add("me", "http://mynamespace/");

                        return xsn;

                    }

                    set { /* needed for xml serialization */ }

                }

     

                public string name;

                public Customer Parent;

            }

     

            [WebMethod]

            public Customer GetCustomer()

            {

                Customer a = new Customer();

                a.name = "a";

                Customer b = new Customer();

                b.name = "b";

                a.Parent = b;

                return a;

            }

     

    Wednesday, September 5, 2007 7:55 PM
    Moderator
  • Wow! You learn something new every day. This has been there since .NET 1.0, and I've never seen it!

     

    Thanks, John!

     

    Wednesday, September 5, 2007 11:48 PM
    Moderator
  • I am trying to add a prefix to the namespace declaration for my webservice but am not having any success doing so.  I have used the code provided above and modified it so that the property xmlns is a property of my webservice class.  I set the property value in the constructor of the webservice class but am not having any luck getting the prefix to show up.

     

    Here is how the namespace declaration appears...

     

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlnsTongue Tiedoap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <processMsg xmlns="http://localhost/B360_CSales_WebService/B360_CSales_WebService.asmx">
          <xmlIn>xml</xmlIn>
        </processMsg>
      </soap:Body>
    </soap:Envelope>

     

     

    and here is how I need to appear...

     

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlnsTongue Tiedoap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <processMsg xmlns:x="http://localhost/B360_CSales_WebService/B360_CSales_WebService.asmx">
          <xmlIn>xml</xmlIn>
        </processMsg>
      </soap:Body>
    </soap:Envelope>

     

     

    Notice the namespace prefix of :x that I am needing to be added.  I have seen a couple examples using the System.Xml.Serialization.XmlNamespaceDeclarationsAttribute class but have not been able to get it to work for me.  I would appreciate any suggestions.

     

    Thanks,

     

    Greg

     

    Thursday, October 4, 2007 2:38 PM