C#, XML: move second namespace to root element

Jonatan

I am trying to create an XML document, using the System.XML.XmlDocument Class.

I have two namespaces in my document.

What my C# code looks like:

XmlDocument xDoc = new XmlDocument();
xDoc.InsertBefore(xDoc.CreateXmlDeclaration("1.0","UTF-8","yes"),xDoc.DocumentElement);
XmlElement root = xDoc.CreateElement('ROOT','http://example.org/ns1');
xDoc.AppendChild(root);
XmlElement child1 = xDoc.CreateElement('CHILD1','http://example.org/ns1');
root.AppendChild(child1);
XmlElement child2 = xDoc.CreateElement('ns2:CHILD2','http://example.com/ns2');
root.AppendChild(child2);
XmlElement child3 = xDoc.CreateElement('ns2:CHILD3','http://example.com/ns2');
root.AppendChild(child3);

Desired output:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ROOT xmlns="http://example.org/ns1" xmlns:ns2="http://example.com/ns2">
    <CHILD1/>
    <ns2:CHILD2/>
    <ns2:CHILD3/>
</ROOT>

Actual output:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ROOT xmlns="http://example.org/ns1">
    <CHILD1/>
    <ns2:CHILD2 xmlns:ns2="http://example.com/ns2"/>
    <ns2:CHILD3 xmlns:ns2="http://example.com/ns2"/>
</ROOT>

Because elements of the second namespace occur multiple times in my document, I don't want this repetitive declaration of the second namespace, but instead have it only once in the root element.

How can I achieve that?

Using LINQ2XML is not an option for me.

Andy Korneyev

Just add all of your desired namespaces as attributes to root element, like

root.SetAttribute("xmlns:ns2", "http://example.com/ns2");

Adding this single line at the end will produce almost exactly your desired output (the only difference is order of xmlns attributes, but I think it doesn't matter in your case):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT xmlns:ns2="http://example.com/ns2" xmlns="http://example.org/ns1">
    <CHILD1 />
    <ns2:CHILD2 />
    <ns2:CHILD3 />
</ROOT>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C#, XML: move second namespace to root element

From Dev

dataweave xml namespace declarations at root element

From Dev

Namespace definition in root element which is also namespaced - valid XML?

From Dev

C# XML Serialization Element namespace

From Dev

WiX remove namespace in root element

From Dev

XML namespace on child element

From Dev

XML namespace on child element

From Dev

Get XML namespace of root node

From Dev

get the namespace prefix and namespace binding of an XML element

From Dev

xml root element missing

From Dev

Adding a root element to XML

From Dev

Adding a root element to XML

From Dev

XML Root Element Parsing

From Dev

Create XML element without namespace

From Dev

Create an XML element with namespace in XMLDocument

From Dev

Create XML element without namespace

From Dev

Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''

From Dev

Why does XmlTypeAttribute.Namespace not set a namespace for the root element?

From Dev

Why does XmlTypeAttribute.Namespace not set a namespace for the root element?

From Dev

XSLT remove root namespace and add namespace to another element

From Dev

Deserialization of XML having multiple namespace in root tag

From Dev

XML schema is resulting in an xml element without a namespace

From Dev

how to remove default namespaces and add custom namespace in root tag of xml using C#?

From Dev

Move every second element to the back of a list, recursively

From Dev

Move XML element using XDT

From Dev

Move XML element using XDT

From Dev

Select the second XML element in an array

From Dev

Adding xml:space to root element

From Dev

Parsing the Root Element of XML with TBXML

Related Related

HotTag

Archive