We help IT Professionals succeed at work.

xml prefixes using linq to xml, c#, vs2008

g_johnson
g_johnson asked
on
The code marked Code1 was taken from this msdn site: http://msdn.microsoft.com/en-us/library/bb387069.aspx and reformatted the way I like to code.

It returns exactly what the msdn site says it will.

In my case, my desired results are:

<UpdateSalesOrder xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea>
    <Sender>
      <LogicalID>ICE.Integration.Adapter.Adnsf.ICE</LogicalID>
    </Sender>
    <BODID>e9f3d9a3-bf34-4740-8a53-c6f45a2a2da3</BODID>
  </ApplicationArea>
  <DataArea>
    <SalesOrder xmlns="http://www.openapplications.org/oagis/9" xmlns:vsite="http://www.vsite.com/vip/oagis/1"/>
      <SalesOrderHeader>
        <DocumentID>
          <ID>100018</ID>
        </DocumentID>
        <Status>
          <Code>40</Code>
          <Description></Description>
          <Reason>Importing</Reason>
        </Status>
        <vsite:ShippedDate>2011-10-19T13:59:14</vsite:ShippedDate>

(and more to follow)

Code 2: shows my attempt at this, but my results look like this:
(notice the prefix error on the last line shown

<UpdateSalesOrder xmlns="http://www.openapplications.org/oagis/9">
  <ApplicationArea>
    <Sender>
      <LogicalID>ICE.Integration.Adapter.Adnsf.ICE</LogicalID>
    </Sender>
    <BODID>266cee16-5357-496b-94f8-e478b5b2330e</BODID>
  </ApplicationArea>
  <DataArea>
    <SalesOrder xmlns="http://www.openapplications.org/oagis/9" xmlns:vsite="http://www.vsite.com/vip/oagis/1"/>
    <SalesOrderHeader>
      <DocumentID>
        <ID>100138</ID>
      </DocumentID>
      <Status>
        <Code>70</Code>
        <Description />
        <REASON>Shipped</REASON>
      </Status>
      <ShippedDate xmlns="http://www.vsite.com/vip/oagis/1">2011-04-22T12:00:00</ShippedDate>

What am I doing wrong?

Thanks in advance; I know this is a lot of code to look at!





Code 1:

                        XNamespace aw = "http://www.adventure-works.com";
                        XNamespace fc = "www.fourthcoffee.com";
                        XElement root = new XElement(aw + "Root");
                        XAttribute attaw = new XAttribute(XNamespace.Xmlns + "aw","http://www.adventure-works.com");
                        XAttribute attfc = new XAttribute(XNamespace.Xmlns + "fc","www.fourthcoffee.com");
                        XElement elChild = new XElement(fc + "Child");
                        XElement elDifChild = new XElement(aw + "DifferentChild","other contecnt");
                        elChild.Add(elDifChild);
                        XElement elC2 = new XElement(aw + "Child2","c2 content");
                        XElement elC3 = new XElement(fc + "Child3","c3 content");

                        root.Add(attaw);
                        root.Add(attfc);
                        root.Add(elChild);
                        root.Add(elC2);
                        root.Add(elC3);


Code 2:

                        //create the xml file
                        XNamespace ns = "http://www.openapplications.org/oagis/9";
                        XNamespace vsite = "vsite://www.vortx.com/vip/oagis/1";
                        //create document
                        XElement USO = new XElement(ns + "UpdateSalesOrder");

                        //create application area element
                        XElement AA = new XElement(ns + "ApplicationArea");


                        // create sender element
                        XElement Sndr = new XElement(ns + "Sender");

                        // create Logical ID element
                        XElement LID = new XElement(ns + "LogicalID");
                        LID.Value = "ICE.Integration.Adapter.Adnsf.ICE";


                        //attach to sender
                        Sndr.Add(LID);


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


                        //add elements to application
                        AA.Add(Sndr);
                        AA.Add(bodid);

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

                        // Sales Order Element and attributes
                        XElement SO = new XElement(ns + "SalesOrder");
                        XAttribute attNS = new XAttribute("xmlns", "http://www.openapplications.org/oagis/9");
                        XAttribute attVortx = new XAttribute(XNamespace.Xmlns + "vsite", "http://www.vsite.com/vip/oagis/1");
                        //attach attributes
                        SO.Add(attNS);
                        SO.Add(attVortx);
                        //sales order header element
                        XElement SOH = new XElement(ns + "SalesOrderHeader");

                        XElement DOCID = new XElement(ns + "DocumentID");

                        //id element
                        XElement elID = new XElement(ns + "ID");
                        elID.Value = stt.Vortx_ord_no;

                        //attach to document id
                        DOCID.Add(elID);



                        //status element
                        XElement STATUS = new XElement(ns + "Status");

                        //code element
                        XElement CODE = new XElement(ns + "Code");
                        CODE.Value = "70";

                        //description element
                        XElement DESCRIPTION = new XElement(ns + "Description");

                        //reason element
                        XElement REASON = new XElement(ns + "REASON");
                        REASON.Value = "Shipped";

                        //add to status element
                        STATUS.Add(CODE);
                        STATUS.Add(DESCRIPTION);
                        STATUS.Add(REASON);
                        //vsite elements
                        //shipped date
                        string sDate = stt.ship_dt.ToString();
                        sDate = sDate.Substring(0, 4) + "-" + sDate.Substring(4, 2) + "-" + sDate.Substring(6) + "T12:00:00";
                        XElement SD = new XElement(vsite + "ShippedDate", sDate);

                        //attach to sales order header
                        SOH.Add(DOCID);
                        SOH.Add(STATUS);
                        SOH.Add(SD);
                        //attached to data area
                        DA.Add(SO);
                        DA.Add(SOH);

                        //attach to USO
                        USO.Add(AA);
                        USO.Add(DA);

                        //save document
                        using (StreamWriter sw = new StreamWriter(this._ftpPath + "UpdateSalesOrder_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml"))
                        {
                            sw.Write(USO.ToString());
                            sw.Flush();
                            sw.Close();
                        }

Open in new window

Comment
Watch Question

Commented:
Isn't it because you do vsite+"ShippedDate" instead of ns+"ShippedDate ?
Commented:
Sorry, I missed the prefix. I think you desired result is not what you expect, otherwise, you do not understand how xmlns works. You defined the xmlns:vsite on a node which does not have any children. the xmlns applies to the scope of the node where you define it and its descendants, not the siblings.

Author

Commented:
I get what you're saying, but ShippingDate is a descendat of SalesOrderHeader which is a descendant of SalesOrder, so I'm confused as to how this breaks that rule.

Commented:
You did not added SalesOrderHeader to the SalesOrder, you added it to the DataArea

Author

Commented:
Wow!  Great catch!  I had been staring at the resulting xml file and had not even caught it that way, and you caugt it apparently by staring at code.  That's impressive.  Everything is working now.  Thanks so much for your help.  It helps, too, to have the explanation as to what was wrong (placement of the declaration).  I can't thank you enough.

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