XML Name Space Not Formating Correctly

Building an XML XDocument to push to web service and the root element needs namespace values this is what the XML form should look like....

<shipment-feed xmlns="http://seller.marketplace.sears.com/oms/v5" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://seller.marketplace.sears.com/oms/v5 asn.xsd ">
<shipment>
<header>
<asn-number>00601780002</asn-number>
<po-number>0060180</po-number>
<po-date>2009-09-26</po-date>
</header>
<detail>
<tracking-number>UPS1XXX</tracking-number>
<ship-date>2001-01-01</ship-date>
<shipping-carrier>UPS</shipping-carrier>
<shipping-method>GROUND</shipping-method>
<package-detail>
<line-number>1</line-number>
<item-id>AB12345678912345456789123456789CD</item-id>
<quantity>1</quantity>
</package-detail>
</detail>
</shipment>
</shipment-feed>

This is the xml that I'm getting....

<?xml version="1.0" encoding="utf-8"?>
<shipment-feed xmlns="http://seller.marketplace.sears.com/oms/v5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsischemalocation="http://seller.marketplace.sears.com/oms/v5 asn.xsd">
  <shipment xmlns="">
    <header>
      <asn-number>2824565201311</asn-number>
      <po-number>2824565</po-number>
      <po-date>2013-11-14</po-date>
    </header>
    <details>
      <tracking-number>579040914892</tracking-number>
      <ship-date>2013-11-14</ship-date>
      <shipping-carrier>FEDEX</shipping-carrier>
      <shipping-method>Ground</shipping-method>
      <package-details>
        <line-number>1</line-number>
        <item-id>LTH7XB1MW-EA</item-id>
        <quantity>3</quantity>
      </package-details>
    </details>
  </shipment>
  <shipment xmlns="">
    <header>
      <asn-number>2821596201311</asn-number>
      <po-number>2821596</po-number>
      <po-date>2013-11-13</po-date>
    </header>
    <details>
      <tracking-number>9405515901119923380663</tracking-number>
      <ship-date>2013-11-14</ship-date>
      <shipping-carrier>USPS</shipping-carrier>
      <shipping-method>Priority Mail</shipping-method>
      <package-details>
        <line-number>1</line-number>
        <item-id>CWD93151-EA</item-id>
        <quantity>6</quantity>
      </package-details>
      <package-details>
        <line-number>2</line-number>
        <item-id>CWD93901-EA</item-id>
        <quantity>4</quantity>
      </package-details>
    </details>
  </shipment>
</shipment-feed>

This is the C# code I created....

XNamespace ns1 = "http://seller.marketplace.sears.com/oms/v5";
XNamespace ns2 = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace ns3 = "http://seller.marketplace.sears.com/oms/v5 asn.xsd";
XDocument doc = new XDocument();    
XElement root = new XElement(ns1 + "shipment-feed",
                             new XAttribute("xmlns" , "http://seller.marketplace.sears.com/oms/v5"),
                             new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                             new XAttribute("xsi" + "schemalocation", "http://seller.marketplace.sears.com/oms/v5 asn.xsd"));


doc.Add(root);
int x = 1;
foreach (SearsOrder s in SearsList)
{
    XElement shipment = new XElement("shipment",                
                        new XElement("header",
                            new XElement("asn-number", s.asnnumber),
                            new XElement("po-number", s.ponumber),
                            new XElement("po-date", s.podate)),
                        new XElement("details",
                            new XElement("tracking-number", s.trackingnum),
                            new XElement("ship-date", s.shipdate),
                            new XElement("shipping-carrier", s.carrier),
                            new XElement("shipping-method", s.method),
                            s.orderitems.Select(i => new XElement("package-details",
                                    new XElement("line-number", x++),
                                    new XElement("item-id", i.itemid),
                                    new XElement("quantity", i.quantity)))                            
            ));

    doc.Root.Add(shipment);
    x = 1;
}

The fist problem is the first child node I'm not seeing where that is coming from because that node is not even declared until the foreach loop. I was under the impression that I was only adding attributes to the root element.

and the other problem is removing the xml declaration

Jon Skeet
people
quotationmark

This sort of the thing is the problem:

new XElement("shipment", ...)

You want the shipment elements to be in the "http://seller.marketplace.sears.com/oms/v5" namespace - so you need to make that explicit. That won't show up in the resulting XML directly, because they'll inherit that as the default namespace specified in the root. Basically, wherever you're creating an element, you probably want to use ns1. So:

new XElement(ns1 + "shipment", new XElement(ns1 + "header", ...), ...)

To understand more about why this is required, you should read up on namespace defaulting in the XML Namespaces specification.

and the other problem is removing the xml declaration

So add an XDeclaration to the XDocument:

XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"))

people

See more on this question at Stackoverflow