2014-05-09 44 views
0

我是新來的處理XML文件,我想知道如何去分裂一個XML文件到兩個文件使用VB。如何拆分vb中的xml文件

我與XML文件有關的主要問題是它太大而無法上傳。我希望將它分成兩個將解決我的問題。例如,如果將一個文件大小爲34kb的xml分成兩份,則會給出2個xml文件,每個文件大小爲17KB。

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> 
+2

你想如何「分裂」它?難道你不能把你需要的任何元素保存到備用文件中嗎? –

+0

這就是我希望對程序做的事情 - 拆分xml文件,然後將其保存爲2個xml文件 – Sash

+0

您想在哪裏拆分它?你想每個''的xml文件?或者你想只保存一個? – Piratica

回答

0

你並不需要將文件當作XML。以純文本處理文件應該沒問題。您可以使用String.Substring方法獲取部分字符串。一個簡單算法的分割可以是這樣的:

  • 定義每個部分將有多長 - Ñ
  • 從位置p(初始爲0)
  • 開始採取 ñ字符從字符串
  • 通過增加pň
  • 循環,直到字符串的結尾前進沒有達到

一個可能解決方案分割字符串分成相等的部分(在此情況下爲2份)可以這樣來實現(在這種情況下,長度取。將一半的字符串長度的=兩個相等的部分):

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 

電話與您希望每個部分有長度的字符串chunkify(你的部分是在列表中包含字符串):

dim content = File.ReadAllText("d:\\xml.xml") 
dim chunks = chunkify(content, content.Length/2) 
for each chunk in chunks 
    Console.WriteLine(chunk) 
next chunk 

與您的內容輸出爲:

<?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> 

我建議您將XML轉換爲字節,然後將字節分成相等的部分(在這種情況下,採取length/2),因爲它可能是適當的轉移。對於分割功能的一種可能的解決辦法是這樣的:

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 

用法如下:

' 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 

使用您的示例XML輸出如預期的那樣拆分後:

<?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> 
==================================