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 ?
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)
)
)
);