Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

How to write an element with a double namespace using linq to xml

I am able to write an element to an xml file using linq that looks like this:
<MyInventoryBalance xmlns="http://www.myplace.com/oagis/9">
by using this C# code:
            XNamespace ns = "http://www.myplace.com/oagis/9";
            XElement SIB = new XElement(ns + "SyncInventoryBalance");

Now I need to write an element that actually uses two namespaces like this:
<InventoryBalance xmlns="http://www.myplace.com/oagis/9" xmlns:second="http://www.mySecond.com/vip/oagis/1">

How would I do that?
Avatar of kaufmed
kaufmed
Flag of United States of America image

In looking at your other question, you should be able to do something like this:

Dim ns As XNamespace = "http://www.myplace.com/oagis/9"
Dim root As New XElement(ns + "MyInventoryBalance", _
						 New XElement("DataArea", _
									  New XElement(ns + "InventoryBalance", _
												   New XAttribute("xmlns", ns), _
												   New XAttribute(XNamespace.Xmlns + "second", ns))))

Open in new window


I paraphrased a bit just to demonstrate what you were after here.
Avatar of g_johnson

ASKER

That didn't seem to work as the namespaces were backwords.  I'll keep trying, though.
ASKER CERTIFIED SOLUTION
Avatar of CuteBug
CuteBug
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CuteBug -- Thank you, I'm getting close.  However, no matter what I try, I cannot get a namespace to attach to the InventoryBalance element.  My code is attached.

The results I'm look for are like this:
<SyncInventoryBalance xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea>
    <Sender>
      <LogicalID>SSO.Integration.Adapter.Smart</LogicalID>
    </Sender>
    <Receiver>
      <LogicalID>SSO.Integration.Adapter.Adnsf.TestAdapter</LogicalID>
    </Receiver>
    <BODID>f133a65a-4214-45cb-9f8a-1770144a65e2</BODID>
  </ApplicationArea>
  <DataArea>
    <InventoryBalance xmlns="http://www.openapplications.org/oagis/9" xmlns:vsite="http://www.vsite.com/vip/oagis/1">
      <Item>
        <ItemID>
          <ID>86727</ID>
        </ItemID>
      </Item>
      <AvailableQuantity>513</AvailableQuantity>
      <vsite:MSRP>19.67</vsite:MSRP>
      <vsite:CostOfGoods>19.67</vsite:CostOfGoods>
    </InventoryBalance>      
  </DataArea>
</SyncInventoryBalance>

And the results I am getting look like this:

<SyncInventoryBalance xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea>
    <Sender>
      <LogicalID>SSO.Integration.Adapter.Smart</LogicalID>
    </Sender>
    <Receiver>
      <LogicalID>SSO.Integration.Adapter.Adnsf.TestAdapter</LogicalID>
    </Receiver>
    <BODID>0a2168aa-405a-48fa-8250-dcf1bb04435b</BODID>
  </ApplicationArea>
  <DataArea>
    <InventoryBalance xmlns:vsite="http://www.vsite.com/vip/oagis/1" />
  </DataArea>
</SyncInventoryBalance>


I tried ns2 in lieu of ns just to attempt to get it to recognize that I need that namespace there.  If I try to add it as an attribute, it is insisting on a name, but of course there is no name for it.

On a different file, I'm going to have to have the same situation but need to attach 9 attributes.

Any thoughts?

private bool SyncInventory()
        {
            bool retVal = true;
            XNamespace ns = "http://www.openapplications.org/oagis/9";
            XNamespace ns2 = "http://www.openapplications.org/oagis/9";
            XNamespace nsV = "http://www.vsite.com/vip/oagis/1";

            try
            {
                foreach (Connections c in this._ConnInfo)
                {
                    XElement SIB = new XElement(ns + "SyncInventoryBalance");

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

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



                    //SENDER ELEMENT
                    XElement Sdr = new XElement(ns + "Sender");

                    //LOGICALID ELEMENT
                    XElement LID = new XElement(ns + "LogicalID");
                    LID.Value = "SSO.Integration.Adapter.Smart";

                    //add logicalid to sender
                    Sdr.Add(LID);

                    //add sender element to application element
                    AA.Add(Sdr);



                    // RECEVIER ELEMENT
                    XElement Rcv = new XElement(ns + "Receiver");

                    //LOGICALID ELEMENT
                    XElement LIDr = new XElement(ns + "LogicalID");
                    LIDr.Value = "SSO.Integration.Adapter.Adnsf.TestAdapter";

                    //add logicalid to receiner
                    Rcv.Add(LIDr);

                    //add receiver element to application element
                    AA.Add(Rcv);


                    //BODID ELEMENT
                    XElement bodid = new XElement(ns + "BODID");
                    bodid.Value = Guid.NewGuid().ToString();


                    //add bodid element to application element
                    AA.Add(bodid);

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



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





                    //********** DATAAREA SEGMENT


                    XElement DA = new XElement(ns +"DataArea");

                    //inventorybalance element
                    //TODO:  trying to figure this one out
                    XElement InvBal = new XElement(ns2 + "InventoryBalance");
                    XAttribute nsAtt = new XAttribute(XNamespace.Xmlns + "vsite", nsV);
                    InvBal.Add(nsAtt);

                    //add inventory balance element to da element
                    DA.Add(InvBal);



                    //add dataarea segment to document
                    SIB.Add(DA);


                    //********** END DATAAREA SEGMENT
                    SIB.Save(this._ftpPath + "SyncInventory_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml");

                }
            }
            catch (Exception ex)
            {
                throw new Exception("At SyncInventory: " + ex.Message);
            }

            return retVal;
        }

Open in new window

Resolved last part of it by sending both as attributes
That didn't seem to work as the namespaces were backwords.
Does it really matter which order they come in? I'm pretty sure XML doesn't care...  I could be wrong.