c# - How to add attribute to a XML node created using Import node -
i'm cloning xml node 1 xml n number of times. identify each node want add index attribute it.
here code use import node.
xmldocument template = new xmldocument(); template.load(filelocation); xmldocument neweventlist= new xmldocument(); xmlnamespacemanager namespaces = new xmlnamespacemanager(template.nametable); namespaces.addnamespace("n", "http://www.hp.com/2009/software/opr/data_model"); xmlnode node = template.selectsinglenode(".//n:event", namespaces); neweventlist.load(eventlist_location); neweventlist.documentelement.appendchild(neweventlist.importnode(template.documentelement, true));
please how can set attribute new node i'm adding.
template document:
<event> <server></server> </event>
output doc:
<eventlist> <event index="0"> <server></server> </event> <event index="1"> <server></server> </event> <event index="2"> <server></server> </event> </eventlist>
before adding node document,
xmlnode node = neweventlist.importnode(template.documentelement, true); //get node xmlattribute attribute= neweventlist.createattribute("index"); // create attribute attribute.value = 0; //set appropriate value node.attributes.append(attribute); // add attribute node
then add node element,
neweventlist.documentelement.appendchild(node);
Comments
Post a Comment