?
Solved

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

Posted on 2014-10-22
4
Medium Priority
?
661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40398096
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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 63

Expert Comment

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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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, …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

743 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