0

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);
.
.
}
Community
  • 1
  • 1
Bhanu Chhabra
  • 474
  • 1
  • 6
  • 22

1 Answers1

0

What you're trying to produce isn't XML for it doesn't conform to XML specification, so don't expect an XML API such as LINQ-to-XML can do that for you. Your best bet might be to produce a proper XML, which include the namespaces definition (maybe using a fake URI if you don't have that information at this point), and then do some string manipulation operations (regex or simple String.Replace()) afterwards to remove those namespaces.

Or maybe this question is just an XY problem, since inserting string that doesn't conform to XML spec, which you intended to do in the next step, can be problematic too, assuming you're still using some kind of XML API for this task.

har07
  • 88,338
  • 12
  • 84
  • 137
  • 0. Yes, I know, the element tag I am trying to producing is not valid XML - only if you consider `` as **the root** of the document. 1. But if you see, it is a valid sub tree for a bigger xml, given the bigger XML is having all the namespace references. – Bhanu Chhabra Mar 23 '17 at 11:27
  • I'm aware of that as it was mentioned in the question. But currently, you're trying to create the `XElement` separately. Nowhere in your code the bigger XML is mentioned, so there is no way it knows the element is supposed to be a part of any other XML – har07 Mar 23 '17 at 11:55
  • If you properly added the `XElement` (not the string representation of it) to the bigger XML you mentioned, your code should've worked i.e namespaces declaration won't be added to `myrootelement` : https://dotnetfiddle.net/U5LHep – har07 Mar 23 '17 at 11:55
  • Sorry for not making it clear enough, but the XElement is sent to a REST API using RestSharp POST call - that is the reason I was saying that I don't have clear idea about the bigger XML. But for an instance let's say I have information about the bigger XML, I can't access it to POST my element at their end. Either I need to get the other party's developers give me another way to insert the XElement in their system, **or there is a way that I yet don't know about**. – Bhanu Chhabra Mar 24 '17 at 06:10