473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Splitting an XML file in C#

I am trying to read an XML file and split it so that it creates a new
file every 1000 records. I can read it, create a new one, but it
creates 1 record per file, rather than 1000. Is there a way of adding
these to the one XML file, then starting a new one when 1000 records
are reached? My code is as follows:

while (XmlReader != null && nodeCounter <= 1001)//(XmlReader != null)
{

// Create a file to write to
XmlTextWriter XmlWriter = new
XmlTextWriter(directory + "\\Output" + x + " CIS_KASN.xml",
null);//System.Text.Encoding.GetEncoding("ISO-8859-1"));
XmlWriter.Formatting = Formatting.Indented;
if (nodeCounter == 1 || nodeCounter == 1001)//%
1000 == 1)
{
XmlWriter.WriteStartDocument();
// Write the header details to the new file
//XmlWriter.WriteProcessingInstruction("xml",
"version='1.0' encoding='ISO-8859-1'");
XmlWriter.WriteComment("This XML message contains
CIS Key Accounts and Special Needs records for inserting/updating in
the IAR");
XmlWriter.WriteStartElement("message");
XmlWriter.WriteAttributeString("xmlns", "xsi",
null, "http://www.w3.org/2001/XMLSchema-instance");
XmlWriter.WriteAttributeString("xsi",
"noNamespaceSchemaLocation", null, "CISKASN.xsd");
}

// Write each row to the file, until 1000 records have
been written
//for (int i = 1; i < 1000; i++)
//{

while (XmlReader.Name != "CISKASNRecord")
{
XmlReader.Read();
}
XmlWriter.WriteNode(XmlReader, true);
XmlWriter.WriteComment("TestNode");
XmlWriter.Flush();
//}

// Tidy up and close the file so that a new one can be
opened
XmlWriter.WriteComment("TestBefore EndElement");
if (nodeCounter == 1 || nodeCounter ==
1001)//nodeCounter % 1000 == 1)
{
XmlWriter.WriteEndElement();
XmlWriter.WriteComment("TestAfter EndElement");
XmlWriter.WriteEndDocument();
}
XmlWriter.Flush();
XmlWriter.Close();
nodeCounter++;
x++;
}
Note: the commented out for-loop is how I thought I could get it to
work, but if I leave this in it creates one file, then crashes halfway
through the second saying the 'message' tag hasn't been closed. Also,
the program doesn't complete (it just hangs).

Can anybody help here please?!

Sep 22 '06 #1
0 3530

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: robsom | last post by:
Hi, I have a problem with a small python program I'm trying to write and I hope somebody may help me. I'm working on tables of this kind: CGA 1988 06 21 13 48 G500-050 D 509.62 J.. R1 1993 01...
4
by: Brice Vissi?re | last post by:
Hello, I would like to handle an XML file structured as following <ROOT> <STEP> .... </STEP> <STEP> .... </STEP>
6
by: nwheavyw8 | last post by:
I am currently trying to write a simple PHP script that will split an uploading file up into 500kb "chunks", then read and concatenate them back together when accessed for download. I can't seem...
1
by: Andy Britcliffe | last post by:
Hi I'm faced with the situation where I could have a single physical file that could contain multiplie XML documents e.g file.txt contains the following: <?xml version="1.0"...
6
by: Earl Anderson | last post by:
I have a A97/XP applet I've developed for my own use in my department. My boss "suggests" that since I built it, I share it with and instruct the other 6 members of my department on its use. I've...
1
by: CaptainWillard | last post by:
Is there any difference between splitting the .mdb vs. linking a table in another .mdb file ? For example, if I use the database splitter tool I end up with Widgets.mdb and Widgets_be.mdb. All the...
2
by: Afifov | last post by:
How can I split a file into several files each with a fixed size? I recall that on linux there is a command that has a counter for file size in bytes but cant seem to remember. Help is...
4
by: nikila | last post by:
Hi, I am trying to split large xml files to smaller xml files using c#.net. can you please provide any sample code for this? I have to split the file if the size is more than 10 MB. Also, xml...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: Steven D'Aprano | last post by:
I'm trying to split a URL into components. For example: URL = 'http://steve:secret@www.domain.com.au:82/dir" + \ 'ectory/file.html;params?query#fragment' (joining the strings above with plus...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.