Search code examples
vb.netlinq-to-xmlxelementsoapheaderxattribute

XElement Automatically Having xmlns attribute


Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
                                        New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                                        New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
                                        New XElement(soap + "Body",
                                        New XAttribute("xmlns", "http://www.test.com"),
                                        New XElement("Open",
                                        New XElement("Data",
                                        New XElement("Desc", _dData.Desc),
                                        New XElement("Num", _dData.Num),
                                        New XElement("Ref", _dData.Ref),
                                        New XElement("Mnn", _dData.Mnn),
                                        New XElement("Ftp", _dData.Ftp))
                                  )))

The following is giving this output:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  <soap:Body xmlns="http://www.test.com">
    <Open xmlns="">
      <Data>
        <Desc>testApp</Desc>
        <Num>1</Num>
        <Ref></Ref>
        <Mnn>116</Mnn>
        <Ftp></Ftp>
      </Data>
    </Open>
  </soap:Body>
</soap:Envelope>

The question is why did the <Open> XElement automatically get an xmlns="" attribute?

I want the same output but without any attribute to the XElement <Open>

Any help would be appreciated.


Solution

  • That's because the XML has default namespace (xmlns="...") declared at <Open> element. In XML, all descendant elements inherits default namespace automatically from the ancestor, unless explicitly set otherwise (f.e by using prefix that points to different namespace, or by declaring different default namespace at the descendant level).

    Using the code you tried, descendants of <Body> are set in no namespace. You need to set descendants of <Open> element to be in the same default namespace by using XNamespace, for example :

    XNamespace ns = "http://www.test.com"
    Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
                                            New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                                            New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
                                            New XElement(soap + "Body",
                                            New XAttribute("xmlns", "http://www.test.com"),
                                            New XElement(ns+"Open",
                                            New XElement(ns+"Data",
                                            New XElement(ns+"Desc", _dData.Desc),
                                            New XElement(ns+"Num", _dData.Num),
                                            New XElement(ns+"Ref", _dData.Ref),
                                            New XElement(ns+"Mnn", _dData.Mnn),
                                            New XElement(ns+"Ftp", _dData.Ftp))
                                      )))