473,386 Members | 1,830 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,386 software developers and data experts.

Splitting an XML doc into smaller ones

3
I am trying to read an XML file and break it up into smaller chunks but am having real problems in doing so as I have never used XML (and am new to C#), so would appreciate any help possible.

then wish to read a large XML document and cycle through it, copying each record to a new file that I have created (with the above header info), until I have 1000 records, then start again. The format of the records is:

<Record>
<attribute1>blah blah</attribute1>
<attribute2>blah blah</attribute2>
<attribute3>blah blah</attribute3>
</Record>

So far I have created the following code:

while (fileReader != null)
{
// Create a file to write to
XmlTextWriter fileWriter = new XmlTextWriter(directory + "\\Output" + x + " myFile.xml", System.Text.Encoding.GetEncoding("ISO-8859-1"));//null);//System.Text.Encoding.UTF8);

// Write the header details to the new file
fileWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'");
fileWriter.WriteStartElement("!-- My comments go here --");
fileWriter.WriteStartElement("message");
fileWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
fileWriter.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "myXSDdoc.xsd");

// Add 'messageHeader' as the element to enclose records in the file
while (fileReader.Name != "messageHeader")
{
fileReader.Read();
}
fileWriter.WriteNode(fileReader, true);

// Write each row to the file, until 1000 records have been written
for (int i = 1; i <= 1001; i++)
{
while (fileReader.Name != "KeyRecord")
fileReader.Read();

fileWriter.WriteNode(fileReader, true);
fileWriter.Flush();
}

// Tidy up and close the file so that a new one can be opened
fileWriter.WriteEndElement();
fileWriter.Close();
x++;
}


I can create a new XML file which prints the header info, then begins to cycle through and print the records as expected (hoped!). However, it crashes at the end with the following error:

"A name was started with an invalid character. Error processing resource 'file:///C:/..."

Also, the second file is created but cannot even be displayed, as it shows the following error:

"XML document must have a top level element. Error processing resource 'file:///C:/..."

Can anybody help please?!
Sep 20 '06 #1
1 1469
samas
3
Possibly a development!

I have increased the size of the for-loop to see what happens in closing the XML document. Now I get a different error:

"The following tags were not closed: message"
where 'message is my main tag.

I guess this is the real reason for my errors, however the C# code looks to close all tags so I can't see how this error is appearing?
Sep 20 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Ahern | last post by:
I'm looking for resources on splitting and merging XML trees. Specifically, on methods to pare large XML documents into smaller documents which can be merged later. Off of the top of my head, I...
9
by: Codex Twin | last post by:
Hi I have a common model for a Data Access Layer scenario. I have an abstract base class, called DalBase which contains a list of abstract methods. Lets call them: public abstract void Shine();...
5
by: Peter Oliphant | last post by:
I was thinking it might be a good idea to split this newsgroup into different newsgroups, depending on the version of VS C++.NET being discussed. Thus, there would be 2002, 2003, and 2005...
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
8
by: Janelle.Dunlap | last post by:
My database is linked to external data from a single Excel spreadsheet. I currently have it so that the entire spreadsheet exports into one table, but really for the purpose of my database it will...
2
by: Jenny | last post by:
Hello All! I have a long XML file that I should transmit to other computer using http. Problem is that the whole XML Document is too large for one transmitting. What is the nicest way to...
2
by: Jenny | last post by:
Hello All! I have a long XML file that I should transmit to other computer using http. Problem is that the whole XML Document is too large for one transmitting. What is the nicest way to...
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...
11
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.