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

Foesenfep

New Member
\[code\] 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>";\[/code\]Among below mentioned two code samples.. usage of childNodes doesn't copy all the child nodes from XML2. only \[code\]<child1>\[/code\] is being copied.\[code\] 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); } } } }\[/code\]code2:\[code\] if (xnNewNode != null) { XmlNodeList xnChildNodes = xnNewNode.ChildNodes; if (xnChildNodes != null) { foreach (XmlNode xnNode in xnChildNodes) { if (xnNode != null) { ParentDoc.DocumentElement.SelectSingleNode(strXpath).AppendChild(xnNode); } } } }\[/code\]ParentDoc.OuterXML after executing first sample of code:\[code\]<Root> <InsertHere> <child1> <childnodes>data</childnodes> <childnodes>data1</childnodes> </child1> <child2> <childnodes>data</childnodes> <childnodes>data1</childnodes> </child2> </InsertHere></Root>\[/code\]ParentDoc.OuterXML after executing second sample of Code\[code\]<Root> <InsertHere> <child1> <childnodes>data</childnodes> <childnodes>data1</childnodes> </child1> </InsertHere></Root>\[/code\]
 
Back
Top