Solved

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

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

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 63

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linq .concat equivalent with expression syntax 4 26
ASP AND XML 3 31
CURL sending XML without spaces PHP 5 39
Web page design problem 3 20
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

696 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