c# - What is the difference between Node.SelectNodes(/*) and Node.childNodes? -


        string xml1 = "<root><inserthere></inserthere></root>";         string xml2 = "<root><child1><childnodes>data</childnodes><childnodes>data1</childnodes></child1><child2><childnodes>data</childnodes><childnodes>data1</childnodes></child2></root>"; 

among below mentioned 2 code samples.. usage of childnodes doesn't copy child nodes xml2. <child1> being copied.

        string strxpath = "/root/inserthere";          xmldocument xdxmlchilddoc = new xmldocument();         xmldocument parentdoc = new xmldocument();         parentdoc.loadxml(xml1);         xdxmlchilddoc.loadxml(xml2);          xmlnode xnnewnode = parentdoc.importnode(xdxmlchilddoc.documentelement.selectsinglenode("/root"), true);          if (xnnewnode != null)         {             xmlnodelist xnchildnodes = xnnewnode.selectnodes("/*");             if (xnchildnodes != null)             {                 foreach (xmlnode xnnode in xnchildnodes)                 {                     if (xnnode != null)                     {                         parentdoc.documentelement.selectsinglenode(strxpath).appendchild(xnnode);                     }                 }             }         } 

code2:

    if (xnnewnode != null)     {         xmlnodelist xnchildnodes = xnnewnode.childnodes;         if (xnchildnodes != null)         {             foreach (xmlnode xnnode in xnchildnodes)             {                 if (xnnode != null)                 {                     parentdoc.documentelement.selectsinglenode(strxpath).appendchild(xnnode);                 }             }         }     } 

parentdoc.outerxml after executing first sample of code:

<root>     <inserthere>         <child1>             <childnodes>data</childnodes>             <childnodes>data1</childnodes>         </child1>         <child2>             <childnodes>data</childnodes>             <childnodes>data1</childnodes>         </child2>     </inserthere> </root> 

parentdoc.outerxml after executing second sample of code

<root>     <inserthere>         <child1>             <childnodes>data</childnodes>             <childnodes>data1</childnodes>         </child1>     </inserthere> </root> 

this clearing of anders g posted, more through explanation.

i surprised foreach not fail (throw exception) in situation, hell.

in code1.
1. create new collection of nodes
2. select nodes it
3. append other node => removing original collection, not newly created one.
4 removing node adding newly collection.

in code2
1. reference original node collection
{child1, child2}
2. append 1st node away collection => removing original collection
{child2} 3. when foreach @ index 1, see passed end of collection. , exit.

this happens lot when changing collection subject iteration.
time, ienumerator throwing exception when such happens.

hope made clear


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -