XML add new element with namespace


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”>

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:
on the new element try new xelement( XNS_ns0 + “Sender”, yourElementValue)

ensure following:

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.

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


Okay i got it somewhat to work


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

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)


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.