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

Notifications

Announcements

No record found.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans