For Reference See: This Question
in the above requirement is to write
<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
<ci:field1>test</ci:field1>
<ca:field2>another test</ca:field2>
</root>
and code for this is given there
But my requirement is to write
<myrootelement>
<ci:field1>test</ci:field1>
<ca:field2 ca:attrb1="value">another test</ca:field2>
</myrootelement>
because this element is going to be part of another big XML which contains own namespace reference for ci and ca. Now "<myrootelement>
" is inserted through api as children to a particular XElement, so I don't have access to XSD or Namespace etc.
What I Tried
XElement element = new XElement("myrootelement",
new XElement(ci + "field1", "test"),
new XElement(ca + "field2", "another test", new XAttribute(ca + "attrb1", "value")));
but that yields:
<myrootelement>
<field1 xmlns="http://somewhere.com">test</field1>
<field2 p2:attrb1="value" xmlns:p2="http://somewhereelse.com" xmlns="http://somewhereelse.com">another test</field2>
</myrootelement>
Edit: Adding further details
The above mentioned element is then sent to Rest API using
{
.
.
var client = new RestClient(uri);
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddParameter("text/xml", elem, ParameterType.RequestBody);
.
.
}