
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)Hello,
I believe you posted your question to wrong forum. Try StackOverflow. This forum is Dynamics CRM dedicated.
Good luck.