问题
I'm new with dealing with xml files and I'm wondering how would i go about splitting an xml file into two files using vb.
The main issue I'm having with the xml file is that its too large to upload. I'm hoping splitting it into two will solve my issue. For example an xml with a file size of 34kb when split into two will give 2 xml files of 17KB each.
Dim doc As XDocument
doc = XDocument.Load("XMLSplit/Directory.xml")
' 1 grab the file size
' 2 divide file size by 2
' 3 find half way of the xml file
' 4 split into two
' 5 save split files as Directory1xml and Directory2.xml
Directory.xml
<Directory>
<Person>
<Name> John / </Name>
<age> 24 </age>
<DOB>
<year> 1990 </year>
<month> 03 </month>
<date> 23 </date>
</DOB>
</Person>
<Person>
<Name> Jane / </Name>
<age> 21 </age>
<DOB>
<year> 1993 </year>
<month> 04 </month>
<date> 25 </date>
</DOB>
</Person>
</Directory>
回答1:
You do not need to treat the file as XML. Handling the file as plain text should be alright. You can use the String.Substring method to take parts of the string. A simple algorithm for splitting can look like this:
- define how long each part would be - n
- take n characters from the string starting from position p (initially 0)
- move forward by increasing p with n
- loop until end of the string is not reached
One possible solution to split the string into equal parts (in this case 2 parts) can be implemented like this (in this case the length to take would be half of the string length = two equal parts):
private function chunkify(byval source as string, byval length as integer) as List(of string)
dim chunks = new List(of string)
dim pos = 0
while (pos < source.Length)
dim toTake = length
if not (source.Length - pos) > length then
toTake = source.Length - pos
end if
chunks.Add(source.Substring(pos, toTake))
pos = pos + length
end while
return chunks
end function
Call the chunkify
on the string with the length you want each part to have (your parts are in a list containing strings):
dim content = File.ReadAllText("d:\\xml.xml")
dim chunks = chunkify(content, content.Length / 2)
for each chunk in chunks
Console.WriteLine(chunk)
next chunk
The output with your content is:
<?xml version="1.0"?>
<Directory>
<Person>
<Name> John / </Name>
<age> 24 </age>
<DOB>
<year> 1990 </year>
<month> 03 </month>
<date> 23 </date>
</DOB>
' here is the new line from the Console.WriteLine
</Person>
<Person>
<Name> Jane / </Name>
<age> 21 </age>
<DOB>
<year> 1993 </year>
<month> 04 </month>
<date> 25 </date>
</DOB>
</Person>
</Directory>
I would suggest you to convert your XML to bytes and then split the bytes into equal parts (in this case, take the length / 2
), because it could be suitable to transfer. One possible solution for the split function could look like this:
function chunkify(byval source as byte(), byval length as integer) as List(Of byte())
' result list containing all parts
dim chunks = new List(of byte())
' the first chunk of content
dim chunk = source.Take(length).ToArray()
do 'loop as long there is something in the array
chunks.Add(chunk)
' remove already read content
source = source.Skip(length).ToArray()
' is there more to take?
chunk = source.Take(length).ToArray()
loop while (chunk.Length > 0)
return chunks
end function
The usage looks like this:
' read all bytes
dim content = File.ReadAllBytes("d:\\xml.xml")
' split into equal parts
dim chunks = chunkify(content, content.Length / 2)
' print / handle each part
for each chunk in chunks
Console.WriteLine(System.Text.Encoding.UTF8.GetString(chunk))
Console.WriteLine("==================================")
next chunk
Using your example XML the output after the split is as expected:
<?xml version="1.0"?>
<Directory>
<Person>
<Name> John / </Name>
<age> 24 </age>
<DOB>
<year> 1990 </year>
<month> 03 </month>
<date> 23 </date>
</DOB>
==================================
</Person>
<Person>
<Name> Jane / </Name>
<age> 21 </age>
<DOB>
<year> 1993 </year>
<month> 04 </month>
<date> 25 </date>
</DOB>
</Person>
</Directory>
==================================
来源:https://stackoverflow.com/questions/23569458/how-to-split-an-xml-file-in-vb