XML add new element with namespace

Hi

How can i add namespace to a new element i.e. ns0:Sender?

I am trying to add a new line with namespace “ns0:”, but when I add new xelement it will not contain the ns0. I am trying with the invoke method add on xedocument with a new xelement(ns, “Sender”, “MS”), but it doesn’t work, as it will allow me to use “:” in a name.

<ns0:Envelope xmlns:ns0=“http://schemas.microsoft.com/dynamics/2008/01/documents/Message”>
ns0:Header
ns0:SourceEndpoint12345</ns0:SourceEndpoint>
ns0:DestinationEndpoint123</ns0:DestinationEndpoint>
ns0:Actionhttp://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create</ns0:Action>
</ns0:Header>

@MarkusDS
give a try on following:
define a variable: XNS_ns0 or any other varname of your choice - datatype is : XNamesspace
set the default value to the namespace value:
http://schemas.microsoft.com/dynamics/2008/01/documents/Message
on the new element try new xelement( XNS_ns0 + “Sender”, yourElementValue)

ensure following:
grafik

for faster exploration playing within watch panel / immediate panel will save time to explore the new XElement statement

Hi ppr

i tried that, but then i get error on namespace is cant not be converted.

@MarkusDS
its not an additional argument
try new XElement(xd+“sender”, YourElementValue)

I prototyped at my End and got it working

Can you provide the workflow file for it?

I get new error when i try

image

Okay i got it somewhat to work

image

but the format is not the same. how can i get ns0:test123</ns0:test>

@MarkusDS
your result is fine so far. Once you add this element to xmldocument where on some parent level the namespace with prefix is defined then it will come as expected.

Have a look here (do debugging to follow the change)
AddNewElementWithNamspace.xaml (5.2 KB)

Hi PPR

Thanks, not sure i understand the connection between myXElement and myRootElement, but it seems the assign of myrootelement also affects myxelement and adds the namespace.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.