We help IT Professionals succeed at work.
Get Started

xml prefixes using linq to xml, c#, vs2008

g_johnson
g_johnson asked
on
428 Views
Last Modified: 2013-11-11
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:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE