Jake
Jake

Reputation: 1781

How do I remove a prefix from an XmlElement but not the attributes?

I'm trying to build an XML document that includes a number of attributes. However the code keeps prefixing the element itself and adds in d1p1 and wrecks all my attributes.

This is what I have so far:

var doc = new XmlDocument();
var declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(declaration);
var xmlns = "http://www.kuju.com/TnT/2003/Delta";
var root = doc.CreateElement("d", "cCSvArray", xmlns);
root.SetAttribute("d", "version", "1.0");
root.SetAttribute("d", "id", "1");
doc.AppendChild(root);

The output is this:

<?xml version="1.0" encoding="utf-8"?>
<d:cCSvArray d1p1:d="1.0" d1p2:d="1" xmlns:d1p2="id" xmlns:d1p1="version" xmlns:d="http://www.kuju.com/TnT/2003/Delta" />

What I need is this:

<?xml version="1.0" encoding="utf-8"?>
<cCSVArray xmlns:d="http://www.kuju.com/TnT/2003/Delta" d:version="1.0" d:id="1"/>

How do I achieve this?

EDIT: The final document should look like this:

<?xml version="1.0" encoding="utf-8"?>
<cCSVArray xmlns:d="http://www.kuju.com/TnT/2003/Delta" d:version="1.0" d:id="1">
    <CSVItem>
        <cCSVItem d:id="2">
            <X d:type="sFloat32">0</X>
            <Y d:type="sFloat32">0</Y>
            <Name d:type="cDeltaString">(80000415004</Name>
        </cCSVItem>
    </CSVItem>
</cCSVArray>

Upvotes: 1

Views: 599

Answers (1)

GSerg
GSerg

Reputation: 78175

var doc = new XmlDocument();

var declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(declaration);

var xmlns = "http://www.kuju.com/TnT/2003/Delta";

var root = doc.CreateElement("cCSvArray");

root.SetAttribute("xmlns:d", xmlns);
root.SetAttribute("version", xmlns, "1.0");
root.SetAttribute("id", xmlns, "1");

doc.AppendChild(root);

Upvotes: 2

Related Questions