21

I need to create an XmlDocument with a root element containing multiple namespaces. Am using C# 2.0 or 3.0

Here is my code:

XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("JOBS", "http://www.example.com");
doc.AppendChild(root);

XmlElement job = doc.CreateElement("JOB", "http://www.example.com");
root.AppendChild(job);  

XmlElement docInputs = doc.CreateElement("JOB", "DOCINPUTS", "http://www.example.com");
job.AppendChild(docInputs);  

XmlElement docInput = doc.CreateElement("JOB", "DOCINPUT", "http://www.example.com");
docInputs.AppendChild(docInput);  

XmlElement docOutput = doc.CreateElement("JOB", "DOCOUTPUT", "http://www.example.com");
docOutputs.AppendChild(docOutput);  

The current output:

<JOBS xmlns="http://www.example.com">
  <JOB>
    <JOB:DOCINPUTS xmlns:JOB="http://www.example.com">
      <JOB:DOCINPUT />
    </JOB:DOCINPUTS>
    <JOB:DOCOUTPUTS xmlns:JOB="http://www.example.com">
      <JOB:DOCOUTPUT />
    </JOB:DOCOUTPUTS>
  </JOB>
</JOBS>

However, my desired output is:

<JOBS xmlns:JOBS="http://www.example.com" xmlns:JOB="http://www.example.com">
  <JOB>
    <JOB:DOCINPUTS>
      <JOB:DOCINPUT />
    </JOB:DOCINPUTS>
  <JOB:DOCOUTPUTS>
    <JOB:DOCOUTPUT />
  </JOB:DOCOUTPUTS>
  </JOB>
</JOBS>

My question: how do I create an XmlDocument that contains a root element with multiple namespaces?

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • Based on your code, it seems your intention is to have have a default namespace of 'http://www.example.com', but the desired output you describe indicates that is not part of a namespace. Can you clarify? – Oppositional Dec 01 '08 at 18:41
  • @Oppositional - inherits the namespace from ; without an explicit namespace, all child elements will inherit from the parent. In this case, the format is dicated by the app AdLib which is expecting the desired output I posted. – Metro Smurf Dec 01 '08 at 18:49

3 Answers3

33

The following will generate the desired output that you requested above:

XmlDocument doc = new XmlDocument();

XmlElement root = doc.CreateElement("JOBS");
root.SetAttribute("xmlns:JOBS", "http://www.example.com");
root.SetAttribute("xmlns:JOB", "http://www.example.com");
doc.AppendChild(root);

XmlElement job = doc.CreateElement("JOB");

XmlElement docInputs    = doc.CreateElement("JOB", "DOCINPUTS", "http://www.example.com");
XmlElement docInput     = doc.CreateElement("JOB", "DOCINPUT", "http://www.example.com");
docInputs.AppendChild(docInput);
job.AppendChild(docInputs);

XmlElement docOutputs   = doc.CreateElement("JOB", "DOCOUTPUTS", "http://www.example.com");
XmlElement docOutput    = doc.CreateElement("JOB", "DOCOUTPUT", "http://www.example.com");
docOutputs.AppendChild(docOutput);
job.AppendChild(docOutputs);

doc.DocumentElement.AppendChild(job);

However, it seems odd that in your example/desired output that the same XML namespace was used against two different prefixes. Hope this helps.

Oppositional
  • 11,141
  • 6
  • 50
  • 63
  • @Oppositional - I agree that it is odd the parent (JOBS) and child (JOB) elements are both explicity declaring the same namespace when just declaring the parent element with the namespace should suffice. – Metro Smurf Dec 02 '08 at 03:27
  • It should suffice. But it doesn't when you're interfacing with an obstinate organization like the IRS who does not perform namespace inheritance properly. – Bon Feb 22 '16 at 20:08
6

You can explicitly create namespace prefix attributes on an element. Then when you add descendant elements that are created with both the same namespace and the same prefix, the XmlDocument will work out that it doesn't need to add a namespace declaration to the element.

Run this example to see how this works:

    using System;
    using System.Xml;

    static void Main(string[] args)
    {
        XmlDocument d = new XmlDocument();
        XmlElement e = d.CreateElement("elm");

        d.AppendChild(e);

        d.DocumentElement.SetAttribute("xmlns:a", "my_namespace");

        e = d.CreateElement("a", "bar", "my_namespace");
        d.DocumentElement.AppendChild(e);
        e = d.CreateElement("a", "baz", "other_namespace");
        d.DocumentElement.AppendChild(e);
        e = d.CreateElement("b", "bar", "my_namespace");
        d.DocumentElement.AppendChild(e);

        d.Save(Console.Out);

        Console.ReadLine();
    }
Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • @Robert - thanks for the explanation; it does clarify the ability to explicity create the namespaces with the SetAttribute method. – Metro Smurf Dec 02 '08 at 14:08
0

try to add the namespace attribute to the root element:

        XmlDocument doc = new XmlDocument();

        XmlElement root = doc.CreateElement("JOBS", "http://www.example.com");
        root.SetAttribute("xmlns:JOB", "http://www.example.com"); 

        doc.AppendChild(root);

        XmlElement job = doc.CreateElement("JOB", "http://www.example.com");
        root.AppendChild(job);

        XmlElement docInputs = doc.CreateElement("JOB", "DOCINPUTS", "http://www.example.com");
        job.AppendChild(docInputs);

        XmlElement docInput = doc.CreateElement("JOB", "DOCINPUT", "http://www.example.com");
        docInputs.AppendChild(docInput);

        XmlElement docOutput = doc.CreateElement("JOB", "DOCOUTPUT", "http://www.example.com");
        root.AppendChild(docOutput);    
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • 1
    @divo - hmm... I wonder if there is any drawback to just creating 2 attributes in the root element using SetAttribute in lieu of expicitly declaring the namespace? – Metro Smurf Dec 01 '08 at 18:54