Solved

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

Posted on 2014-10-22
4
530 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 40

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
User Authentication using Digital Certificate 2 44
SQL Result Set to XML 4 36
Jquery/JSON passing value from one function to another 12 35
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

17 Experts available now in Live!

Get 1:1 Help Now