c# 4.0 - Insert XML to XML Document at specified tag -


i working on xml want insert below xml tag parent xml:

<subject></subject> 

parent xml

<school>    <classb></classb>    <classa>       <students>       </students>    </classa> </school> 

want final out put

<school>    <classb></classb>    <classa>       <subject></subject>       <students>       </students>    </classa> </school> 

tried below code:

xmldocument xmlrequest = new xmldocument();  xmlrequest.loadxml(parentxml);  xmldocumentfragment xmlfrag = xmlrequest.createdocumentfragment();  xmlfrag.innerxml = xmlsubjects;  xmlrequest.documentelement.insertbefore(xmlfrag,  xmlrequest.documentelement.firstchild); 

this insert subject element right after school. how insert @ specific path. in case under classa element.

please me here.

you need use getelementbyname or method classa node:

xmlnode xnode = xmlrequest.selectsinglenode("school/classa"); 

then add subject new child node:

xmlnode newnode = xmlrequest.createnode(xmlnodetype.element, "subject", nothing); xnode.appendchild(newnode); 

if want add in specific place, enumerate parent node , existing node, , use insertbefore:

xmlnode xnode = xmlrequest.selectsinglenode("school/classa"); xmlnode siblingnode = xmlrequest.selectsinglenode("school/classa/students"); xmlnode newnode = xmlrequest.createnode(xmlnodetype.element, "subject", nothing); xnode.insertbefore(xnode, xsibling); 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -