Search code examples
c#xmlxml-parsingxmldocument

Attribute Name overlapping in setattribute property of Xelement


I am trying to re-build an XMI structure. So for that i need to append a child node like below

 <node xmi:type="shape" xmi:id="12358" type="rectangle">
 </node >

So created an XMLElement and tried to add attributes using below code

 XmlElement child= papNotdoc.CreateElement("node");
 child.SetAttribute("type", "http://www.omg.org/XMI", "Shape");
 child.SetAttribute("id", "http://www.omg.org/XMI","12358");
 child.SetAttribute("type", "rectangle");

using the namespace URL so that i will get a prefix XMI: in one of the type attribute.

But unfortunately XML element treats both type named attributes as same attributes and giving me an output as below

 <node xmi:type="rectangle" xmi:id="12358">
 </node >

I want both the xmi:type and type attribute in a node.How to achieve it ?


Solution

  • You should give null to namespaceURI param of SetAttribute method.

    XmlElement child = papNotdoc.CreateElement("node");
    child.SetAttribute("type", "http://www.omg.org/XMI", "Shape");
    child.SetAttribute("id", "http://www.omg.org/XMI", "12358");
    child.SetAttribute("type", null, "rectangle");