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

  • Home
  • SEARCH
  • 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 6164141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:56:35+00:00 2026-05-23T21:56:35+00:00

Edit: I decided to take the LINQ to XML approach (see the answer below)

  • 0

Edit: I decided to take the LINQ to XML approach (see the answer below) that was recommended and everything works EXCEPT that I can’t replace out the changed records with the records from the incremental file. I managed to make the program work by just removing the full file node and then adding in the incremental node. Is there a way to just swap them instead? Also, while this solution is very nice, is there any way to shrink down memory usage without losing the LINQ code? This solution may still work, but I would be willing to sacrifice time to lower memory usage.


I’m trying to take two XML files (a full file and an incremental file) and merge them together. The XML file looks like this:

<List>
    <Records>
        <Person id="001" recordaction="add">
            ...
        </Person>
    </Records>
</List>

The recordaction attribute can also be “chg” for changes or “del” for deletes. The basic logic of my program is:

1) Read the full file into an XmlDocument.

2) Read the incremental file into an XmlDocument, select the nodes using XmlDocument.SelectNodes(), place those nodes into a dictionary for easier searching.

3) Select all the nodes in the full file, loop through and check each against the dictionary containing the incremental records. If recordaction=”chg” or “del” add the node to a list, then delete all the nodes from the XmlNodeList that are in that list. Finally, add recordaction=”chg” or “add” records from the incremental file into the full file.

4) Save the XML file.

I’m having some serious problems with step 3. Here’s the code for that function:

private void ProcessChanges(XmlNodeList nodeList, Dictionary<string, XmlNode> dictNodes)
    {
        XmlNode lastNode = null;
        XmlNode currentNode = null;
        List<XmlNode> nodesToBeDeleted = new List<XmlNode>();

        // If node from full file matches to incremental record and is change or delete, 
        // mark full record to be deleted.
        foreach (XmlNode fullNode in fullDocument.SelectNodes("/List/Records/Person"))
        {
            dictNodes.TryGetValue(fullNode.Attributes[0].Value, out currentNode);
            if (currentNode != null)
            {
                if (currentNode.Attributes["recordaction"].Value == "chg"
                    || currentNode.Attributes["recordaction"].Value == "del")
                {
                    nodesToBeDeleted.Add(currentNode);
                }
            }
            lastNode = fullNode;
        }

        // Delete marked records
        for (int i = nodeList.Count - 1; i >= 0; i--)
        {
            if(nodesToBeDeleted.Contains(nodeList[i]))
            {
                nodeList[i].ParentNode.RemoveChild(nodesToBeDeleted[i]);
            }
        }

        // Add in the incremental records to the new full file for records marked add or change.
        foreach (XmlNode weeklyNode in nodeList)
        {
            if (weeklyNode.Attributes["recordaction"].Value == "add"
                || weeklyNode.Attributes["recordaction"].Value == "chg")
            {
                fullDocument.InsertAfter(weeklyNode, lastNode);
                lastNode = weeklyNode;
            }
        }
    }

The XmlNodeList being passed in is just all of the incremental records that were selected out from the incremental file, and the dictionary is just those same nodes but key’d on the id so I didn’t have to loop through all of the incremental records each time. Right now the program is dying at the “Delete marked records” stage due to indexing out of bounds. I’m pretty sure the “Add in the incremental records” doesn’t work either. Any ideas? Also some suggestions on making this more efficient would be nice. I could potentially run into a problem because it’s reading in a 250MB file which balloons up to 750MB in memory, so I was wondering if there was an easier way to go node-by-node in the full file. Thanks!

  • 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-05-23T21:56:36+00:00Added an answer on May 23, 2026 at 9:56 pm

    Here’s an example of how you might accomplish it with LINQ-to-XML. No dictionary is needed:

    using System.Xml.Linq;
    
    // Load the main and incremental xml files into XDocuments
    XDocument fullFile = XDocument.Load("fullfilename.xml");
    XDocument incrementalFile = XDocument.Load("incrementalfilename.xml");    
    
    // For each Person in the incremental file
    foreach (XElement person in incrementalFile.Descendants("Person")) {
    
        // If the person should be added to the full file
        if (person.Attribute("recordaction").Value == "add") {
            fullFile.Element("List").Element("Records").Add(person); // Add him
        }
    
        // Else the person already exists in the full file
        else {
            // Find the element of the Person to delete or change
            var personToChange =
                    (from p in fullFile.Descendants("Person")
                        where p.Attribute("id").Value == person.Attribute("id").Value
                        select p).Single();
    
            // Perform the appropriate operation
            switch (person.Attribute("recordaction").Value) {
                case "chg":
                    personToChange.ReplaceWith(person);
                    break;
                case "del":
                    personToChange.Remove();
                    break;
                default:
                    throw new ApplicationException("Unrecognized attribute");
            }
        }
    }// end foreach
    
    // Save the changes to the full file
    fullFile.Save("fullfilename.xml");
    

    Please let me know if you have any problems running it and I’ll edit and fix it. I’m pretty sure it’s correct, but don’t have VS available at the moment.

    EDIT: fixed the "chg" case to use personToChange.ReplaceWith(person) rather than personToChange = person. The latter doesn’t replace anything, as it just shifts the reference away from the underlying document.

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

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
Edit: From another question I provided an answer that has links to a lot
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
EDIT: Decided tp use the patch utility and generated the SQL from there using
Edit : I decided to just convert this to a normal web page, as
I've decided to take the advice in this question: create-excel-chart-programmatically-in-php and NOT attempt to
I was over at the StarCraft2 website and decided to take a look at
So I decided to take a look at Smalltalk. Googling led me to Squeak
Edit: This question was written in 2008, which was like 3 internet ages ago.

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.