web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to split a xml file that has many child nodes into multiple files

(1) ShareShare
ReportReport
Posted on by 255

I have a requirement to split a 100 mb file into 10 parts. I have 3 childnodes and 1 Root node. The following code is taking only 1 child node and a root node. But, i have also to add 2 other child nodes.

static void Main(string[] args)
{
string sourceFile = @"Desktop\123.xml";
string rootElement = "MessageBody";
string descElement = "FSContract";
int take = 200;
string destFilePrefix = "FSContract";
string destPath = @"Desktop\";

SplitXmlFile(sourceFile, rootElement, descElement, take,
destFilePrefix, destPath);

Console.ReadLine();
}

private static void SplitXmlFile(string sourceFile
, string rootElement
, string descendantElement
, int takeElements
, string destFilePrefix
, string destPath)
{
XElement xml = XElement.Load(sourceFile);
// Child elements from source file to split by.
var childNodes = xml.Descendants(descendantElement);

// This is the total number of elements to be sliced up into
// separate files.
int cnt = childNodes.Count();

var skip = 0;
var take = takeElements;
var fileno = 0;

// Split elements into chunks and save to disk.
while (skip < cnt)
{
// Extract portion of the xml elements.
var c1 = childNodes
.Skip(skip)
.Take(take);

// Setup number of elements to skip on next iteration.
skip += take;
// File sequence no for split file.
fileno += 1;
// Filename for split file.
var filename = String.Format(destFilePrefix + "_{0}.xml", fileno);
// Create a partial xml document.

XElement split = new XElement(rootElement, c1);
// Save to disk.
split.Save(destPath + filename);
}
}

Please help.

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe you posted your question to wrong forum. Try StackOverflow. This forum is Dynamics CRM dedicated.

    Good luck.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans