We help IT Professionals succeed at work.

namespace mystery, linq to xml, VS2008, c#

g_johnson
g_johnson asked
on
This code:

                    XNamespace ns = "http://www.openapplications.org/oagis/9";

                    XElement SIB = new XElement(ns + "SyncInventoryBalance");

                    //********** APPLICATION SEGMENT

                    XName aplA = "ApplicationArea";
                    XElement AA = new XElement(aplA);

                    //add application segment to document
                    SIB.Add(AA);


produces this result:

<SyncInventoryBalance xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea xmlns="" />
</SyncInventoryBalance>

Why am I seeing "xmlns" in the ApplicationArea Element?  I don't want it there.


Thanks,
Comment
Watch Question

Fernando SotoRetired
SILVER EXPERT
Distinguished Expert 2017

Commented:
Hi g_johnson;

To your question, "Why am I seeing "xmlns" in the ApplicationArea Element?  I don't want it there.
", the reason is you are adding it there by assigning the namespace to the node. If you do not want it there do the following.

XElement SIB = new XElement("SyncInventoryBalance");

XName aplA = "ApplicationArea";
XElement AA = new XElement(aplA);

//add application segment to document
SIB.Add(AA);

Open in new window


Will result in xml as follows:

<SyncInventoryBalance>
  <ApplicationArea />
</SyncInventoryBalance> 

Open in new window


Fernando

Commented:
The XML that you obtain states that the element SyncInventoryBalance belongs to the namespace "http://www.openapplications.org/oagis/9". But the element ApplicationArea does not. That is why its namespace is shown as an empty string.
To fix this you must specify that the element ApplicationArea also belongs to the namespace "http://www.openapplications.org/oagis/9".

What you need to do is to add the namespace ns to aplA. Check the code below.

This will give the following xml:

<SyncInventoryBalance xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea />
</SyncInventoryBalance>
XNamespace ns = "http://www.openapplications.org/oagis/9";

XElement SIB = new XElement(ns + "SyncInventoryBalance");

//********** APPLICATION SEGMENT

XName aplA = ns + "ApplicationArea";
XElement AA = new XElement(aplA);

//add application segment to document
SIB.Add(AA);

Open in new window

Author

Commented:
Thank you.  If you have time could you check out this question on double (and what turns out to be multiple) namespaces on a single element.

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27428507.html

Explore More ContentExplore courses, solutions, and other research materials related to this topic.