【问题标题】:How to split an xml file in vb如何在vb中拆分xml文件
【发布时间】:2014-05-09 16:17:53
【问题描述】:

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

xml 文件的主要问题是它太大而无法上传。我希望将它分成两部分来解决我的问题。例如,将文件大小为 34kb 的 xml 拆分为两个时将产生 2 个 17KB 的 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

目录.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 文件,然后将其另存为 2 个 xml 文件
  • 你想在哪里拆分它?您想要每个&lt;person&gt; 的xml 文件吗?还是您只想保存一个?
  • 您要在每个Person 节点上拆分(为每个人生成两个单独的文件)吗?
  • “我希望程序接收文件 - 将文件大小除以 2 然后拆分文件 - 这样两个文件将生成 1 个 Directory.xml 文件” - 抱歉,但这没有任何意义。

标签: xml vb.net xmlnode


【解决方案1】:

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

  • 定义每个部分的长度 - n
  • 从位置 p(最初为 0)开始的字符串中取 n 个字符
  • 通过将 p 增加为 n 继续前进
  • 循环直到未到达字符串末尾

将字符串分成相等的部分(在这种情况下为 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>
==================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多