-->

如何分割在VB中的XML文件(How to split an xml file in vb)

2019-11-03 05:12发布

我是新与处理XML文件,我不知道如何将我去拆分一个XML文件到用vb两个文件。

我与XML文件具有的主要问题是,它太大,无法上传。 我希望它分成两个能解决我的问题。 例如具有34KB的文件大小的XML时分成两会给每个17KB的2个XML文件。

   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>

Answer 1:

你并不需要将文件当作XML。 处理该文件作为纯文本应该是正常的。 您可以使用String.Substring法取部分字符串。 一个简单的算法拆分可以是这样的:

  • 定义每个部分将有多长- N
  • n从字符串中的字符从位置p开始(最初为0)
  • 前进与N 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>
==================================


文章来源: How to split an xml file in vb