Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:15:18+00:00 2026-06-06T20:15:18+00:00

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 two code samples..

  • 0
        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 two code samples.. usage of childNodes doesn’t copy all the child nodes from XML2. only <child1> is 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:15:19+00:00Added an answer on June 6, 2026 at 8:15 pm

    this is a clearing of what Anders G posted, with more through explanation.

    I am surprised the foreach does not fail (Throw Exception) in this situation, but hell.

    In code1.
    1. Create a NEW COLLECTION of nodes
    2. Select nodes to it
    3. append to other node => removing from original collection, but not the newly created one.
    4 you are removing the node you are adding from the newly collection.

    in Code2
    1. Reference the ORIGINAL node collection
    {child1, child2}
    2. append 1st Node away to another collection => removing it from the original collection
    {child2}
    3. now when the foreach at index 1, it see that it passed the end of the collection. and exit.

    this happens a lot when changing a collection that is subject to iteration.
    but most the time, the IEnumerator is throwing an Exception when such happens.

    hope i made it all clear

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two string of xml1 and xml2 which both represent xml in
the following code crashes.Basically i am accessing a very large string(xml -contains image data)
string abc = 07:00 - 19:00 x = int.Parse(only first two characters) // should
string s = <script type=text/javascript src=/stepcarousel.js></script> How can I get the data inside the
The following method below works perfectly if $output is a string. Although in a
String s1 = Project; String s2 = Sunject; I want to compare the two
Can anyone explain to me why xml1.Element(title) correctly equals <title>Customers Main333</title> but xml2.Element(title) surprisingly
String htmlCodeGoesHere=<div id=\fb-root\></div>+ <script>(function(d, s, id) {+ var js, fjs = d.getElementsByTagName(s)[0];+ if (d.getElementById(id))
string strConn = Provider=Microsoft.Ace.OLEDB.12.0;Data Source= + filename + ;Extended Properties=\Excel 12.0 Xml;HDR=Yes;IMEX=1\;; I search
string myTagData = some valid xml data; I want to write the LINQ to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.