Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multiple namespaces to the root element with XmlDocument?

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?

like image 850
Metro Smurf Avatar asked Dec 01 '08 17:12

Metro Smurf


People also ask

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

How do I add a namespace to an XML file?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

Can XML attributes have namespaces?

Similarly, a namespace in XML can have many reusable elements and attributes. To use a class or interface in a package, you must fully qualify that class or interface with the package name. Similarly, to use an element or attribute in a namespace, you must fully qualify that element or attribute with the namespace.


2 Answers

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.

like image 122
Oppositional Avatar answered Oct 17 '22 20:10

Oppositional


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();
    }
like image 26
Robert Rossney Avatar answered Oct 17 '22 21:10

Robert Rossney