如何在vb中拆分xml文件 [英] How to split an xml file in vb

查看:34
本文介绍了如何在vb中拆分xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是处理 xml 文件的新手,我想知道如何使用 vb 将 xml 文件拆分为两个文件.

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.

我对 xml 文件的主要问题是它太大而无法上传.我希望把它分成两部分能解决我的问题.例如,文件大小为 34kb 的 xml 拆分为两个时,将提供 2 个 17KB 的 xml 文件.

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

目录.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>

推荐答案

您不需要将文件视为 XML.将文件作为纯文本处理应该没问题.您可以使用 String.Substring取字符串的一部分的方法.一个简单的分割算法如下所示:

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:

  • 定义每个部分的长度 - n
  • 从位置p(初始为0)开始的字符串中取n个字符
  • 通过用 n
  • 增加 p 来向前移动
  • 循环直到未到达字符串末尾
  • 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

将字符串分成相等部分(在这种情况下为 2 部分)的一种可能解决方案可以这样实现(在这种情况下,要采用的长度将是字符串长度的一半 = 两个相等部分):

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

在字符串上调用 chunkify ,其长度为您希望每个部分的长度(您的部分位于包含字符串的列表中):

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

您的内容的输出是:

<?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),因为它可能适合传输.split 函数的一种可能解决方案可能如下所示:

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

用法如下:

' 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,拆分后的输出符合预期:

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

这篇关于如何在vb中拆分xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆