Solved

How do I create an XML file with multiple namespaces using C#

Posted on 2014-10-22
4
497 Views
Last Modified: 2014-10-23
I need to produce the following XML file using linq to XML, and the Namespaces and schemalocation are killing me!  After hours of research and trial and error, I'm hoping you can help.

As you can see, I need to loop through the product after a table read.

If it weren't for the namespaces and schemalocation, I would do this:

                dcROIdc dc = new dcROIdc(this._ConnString);

                XDocument doc = new XDocument();
                XElement feed = new XElement("feed");


                for (int i = 0; i < 10; i++)
                {
                    XElement product = new XElement("product");
                    XElement SKU = new XElement("SKU");
                    SKU.Value = "sku_" + i.ToString();
                    XElement inventory = new XElement("inventory");
                    XElement quantity = new XElement("quantity");
                    quantity.Value = i.ToString();
                    inventory.Add(quantity);
                    product.Add(SKU);
                    product.Add(inventory);
                    feed.Add(product);
                }

                doc.Add(feed);

and then save the document.

How do I get the namespaces and schemalocation in there?

Thanks!


<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.junk.com/xml/mp/inventory/R1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.junk.com/xml/mp/inventory/R1.1 http://support.junk.com/mp/R1.1/product/inventory.xsd">
  <product>
    <SKU>sku_1</SKU>
    <inventory>
      <quantity>1</quantity>
    </inventory>
  </product>
  <product>
    <SKU>sku_2</SKU>
    <inventory>
      <quantity>12</quantity>
    </inventory>
  </product>
</feed>
0
Comment
Question by:g_johnson
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 40398096
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40398516
Hi  g_johnson;

The following sample code should do what you need.

// Create the needed XNamespace's
XNamespace ns = "http://www.junk.com/xml/mp/inventory/R1.1";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace schemaLocation = "http://www.junk.com/xml/mp/inventory/R1.1 http://support.junk.com/mp/R1.1/product/inventory.xsd";
// Create XDocument with XDocument and Root node
XDocument feed = new XDocument(new XDeclaration("1.0", "utf-8", null), 
    new XElement(ns + "feed",
        new XAttribute("xmlns", "http://www.junk.com/xml/mp/inventory/R1.1"),
        new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        new XAttribute(xsi + "schemaLocation", schemaLocation) 
             ));
// Fill the XDocument with the needed XElements             
for (int i = 0; i < 10; i++)
{
    XElement product = new XElement(ns + "product",
        new XElement(ns + "SKU"),
        new XElement(ns + "inventory",
        new XElement(ns + "quantity", i.ToString())
        ));
    
    feed.Root.Add(product);
}

// Save the XDocument to the file system.
feed.Save("File path and file name");

Open in new window

0
 
LVL 4

Author Comment

by:g_johnson
ID: 40399573
Thank you.  That works.  Now I'm studying the code to see if I can understand it!!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40399673
Not a problem g_johnson, glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now