Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

25 Experts available now in Live!

Get 1:1 Help Now