Search code examples
c#wcfsoapcorewcf

Parsing header elements without any namespace


I am trying to build a CoreWCF service that can parse an XML request like the following -

<NS1:Envelope
    xmlns:NS1="http://schemas.xmlsoap.org/soap/envelope/">
    <NS1:Header>
        <Tag1>Value 1</Tag2>
        <Tag2>Value 2</Tag2>
    </NS1:Header>
    <NS1:Body>
    <!-- Body elements here -->
    </NS1:Body>
</NS1:Envelope>

So, I have declared a MessageContract similar to this -

[MessageContract]
public class Class1
{
    [MessageHeader]
    public string Tag1 { get; set; }
    
    [MessageHeader]
    public string Tag2 { get; set; }
    
    // other body tag declaration
}

However when the request is received in my service, it is unable to parse the values of Tag1 and Tag2 as there are no namespace given in the request in those tags.

I tried to set [XmlElement(Form=XmlSchemaForm.Unqualified)] to the properties like below -

[MessageContract]
public class Class1
{
    [MessageHeader]
    [XmlElement(Form=XmlSchemaForm.Unqualified)]
    public string Tag1 { get; set; }
    
    [MessageHeader]
    [XmlElement(Form=XmlSchemaForm.Unqualified)]
    public string Tag2 { get; set; }
    
    // other body tag declaration
}

but this throws error when I try to visit the WSDL page from browser I get following error -

elements declared at the top level of a schema cannot be unqualified

Can anyone suggest how I can parse these values without namespace. The request format cannot be changed.


Solution

  • From my understanding, there should be no namespace in your request, but there is in the service.

    You can try removing the XML namespace by setting the Namespace parameter of the MessageHeader property to an empty string, as follows:

    public class Class1
     {
       
         [MessageHeader(Namespace = "")]
         public string Tag1 { get; set; }
      
         [MessageHeader(Namespace = "")]
         public string Tag2 { get; set; }
    
         // other body tag declaration
     }