0

i am building an XML using XDocument.this is my code

        var ns = XNamespace.Get("url");        
    XDocument requestXMl = new XDocument(
        new XElement(ns+"WEB_REQUEST",
            new XElement("HTTP_HEADER_INFORMATION",
                new XElement("DEFINED_HEADERS",
                    new XElement("HTTP_DEFINED_REQUEST_HEADER",
                            new XElement("ItemNameType", "RequestDate"),
                            new XElement("ItemValue", _currentTime)
                                ),
                        new XElement("HTTP_DEFINED_REQUEST_HEADER",
                            new XElement("ItemNameType", "AuthorizationValue"),
                            new XElement("ItemValue", credentials)
                                )
                              )
                           ),
            new XElement("COLL",
                new XElement("TID", _t),
                new XElement("SID", _s)
                        )
                    )
            );

the output for this code is

  <WEB_REQUEST xmlns="url">
  <HTTP_HEADER_INFORMATION xmlns="">
    <DEFINED_HEADERS>
      <HTTP_DEFINED_REQUEST_HEADER>
        <ItemNameType>RequestDate</ItemNameType>
        <ItemValue>Wed,06 May 2015 18:14:33 GMT</ItemValue>
      </HTTP_DEFINED_REQUEST_HEADER>
      <HTTP_DEFINED_REQUEST_HEADER>
        <ItemNameType>AuthorizationValue</ItemNameType>
        <ItemValue>ieuKB5voR3w==</ItemValue>
      </HTTP_DEFINED_REQUEST_HEADER>
    </DEFINED_HEADERS>
  </HTTP_HEADER_INFORMATION>
  <COLL xmlns="">
    <TID></TID>
    <SID></SID>
  </COLL>
</WEB_REQUEST>

I don't want the xmlns to appear 3 times as it appears in the output. I want it to appear only 2 times.

<WEB_REQUEST xmlns="url"> 
  <COLLABORATION xmlns="">

how can i achieve this ?

crthompson
  • 15,653
  • 6
  • 58
  • 80
grace
  • 253
  • 1
  • 5
  • 17

1 Answers1

0

Specify "url" namespace for the element you don't want to have an empty xmlns. This clears all the xml namespaces except for the root element:

var ns = XNamespace.Get("url");        
XDocument requestXMl = new XDocument(
    new XElement(ns+"WEB_REQUEST",
        new XElement(ns+"HTTP_HEADER_INFORMATION",
            new XElement(ns+"DEFINED_HEADERS",
                new XElement(ns+"HTTP_DEFINED_REQUEST_HEADER",
                        new XElement(ns+"ItemNameType", "RequestDate"),
                        new XElement(ns+"ItemValue", _currentTime)
                            ),
                    new XElement(ns+"HTTP_DEFINED_REQUEST_HEADER",
                        new XElement(ns+"ItemNameType", "AuthorizationValue"),
                        new XElement(ns+"ItemValue", credentials)
                            )
                          )
                       ),
        new XElement(ns + "COLL",
            new XElement(ns + "TID", _t),
            new XElement(ns + "SID", _s)
                    )
                )
        );
Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
  • this still adds empty xmns to new the second element. I don't want that..How can i remove this empty XMNS form the second element ? – grace May 06 '15 at 18:55
  • 1
    you can add ns+ to the element name and it's descendent as I did for the COLL element, but I guess there may be a more elegant way. – Mehrzad Chehraz May 06 '15 at 18:57
  • its adding empty xmns to the next element if i add ns+ to the element. its looks like a bandaid. I hoping there is better way doing it. I dont want to add ns+ to every element i dont want empty space. Anyone with better solution ? – grace May 06 '15 at 19:11
  • Seems like that there is no elegant way to do that but you may use a foreach loop to set namespace of all of the elements. see [How to set the default XML namespace for an XDocument](http://stackoverflow.com/a/2874572/3956290) – Mehrzad Chehraz May 06 '15 at 19:16