将我的集合和对象写入磁盘并在以后重新导入的最佳策略是什么? [英] What is the best strategy for writing my collections and objects to disk and reimporting them later?

查看:110
本文介绍了将我的集合和对象写入磁盘并在以后重新导入的最佳策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些设计的集合和对象,它们具有四层深度的一对多关系。 CollectionA 包含一些 CollectionB 对象和 CollectionB 包含一个 CollectionC 对象和 CollectionC 包含一些内容对象。



在用户花费时间填充所有这些集合和对象与必要的数据之后,他们需要能够保存每个对象的所有属性,然后读取输出文件并启动它们离开的位置。



我知道这是一个有些宽泛的问题,所以我希望看看在研究中应该如何处理这个问题。现在我最关心的是文件I / O,我在想两条不同的路线。



这是一个excel项目,所以我想我可以从上往下循环遍历所有的集合和对象,并将其值写入电子表格,然后循环遍历电子表格行并将其输出到分隔的文本文件。然后反过来读取文本文件 - 从文本到电子表格,然后从电子表格到集合和对象。



也许另一个选项是绕过电子表格并使用TextStream?如果我计划在未来的某个时候将VBA应用程序转换为独立的VB.NET,那将是可能的吗?

解决方案

一个好的选择是将您的集合存储为XML文件。之后添加或阅读个别元素比较容易;并且您将保存自己的索引噩梦,如果您尝试将四维数据排列到二维电子表格上,这将会不可避免地发生。它们是文本文件,因此人类可读;如果文件大小变成问题,只需压缩它们。



您只需设计一个适当的格式,即标签名称等,请参阅本答案末尾的示例。



编辑:如果将数据打包到Excel工作簿中对您来说非常重要,那么您可以将xml代码放在其中一张表上,并将其存储在...之前。



创建如下所示的XML文档:

 设置xmlFatAssCollectionA = CreateObject(Msxml2.DOMDocument.6.0)

然后编辑它:循环遍历您的集合,附加每个元素作为较大集合的孩子,酌情。完成后,保存XML文档。

  xmlFatAssCollectionA.save(C:\MyDir\MyCollection.xml)

文档: http://msdn.microsoft.com/en-us/library/ms756987%28v=VS.85%29.aspx



您可以使用XPath访问所需的节点/元素。 XPath教程



这是什么您生成的xml文件可能看起来像。我只添加了一个collectionB对象,省略了整个collectionA层,但是你得到了这个想法。

 <?xml version =1.0encoding =ISO-8859-1?> 

< bObject>
< cObject>
< contentObject>
< title lang =da> Kejserens nyeklæder< / title>
< author> H.C。安徒生< /作者>
< price> priceless< / price>
< / contentObject>
< / cObject>
< cObject>
< contentObject>
< title lang =en>哈利波特< / title>
< author> J K. Rowling< / author>
< year> 2005< / year>
< price> 29.99< / price>
< / contentObject>
< contentObject>
< title lang =fr> Les Schtroumpfs< / title>
< author>某些人< / author>
< year> 1985< / year>
< / contentObject>
< / cObject>
< / bObject>


I have a number of collections and objects set up that have a one-to-many relationship that is four levels deep. CollectionA contains a number of CollectionB objects and CollectionB contains a number of CollectionC objects and CollectionC contains a number of Content objects.

After the user has spent time populating all of these collections and objects with the necessary data, they need to be able to save all of the properties of each object and then read the output file and start where they left off.

I know this is a somewhat broad question so I'm looking to see which direction I should head in my research on how to do this. Right now I'm most concerned with the File I/O and I'm thinking of two different routes.

It's an excel project, so I was thinking I could loop through all of the Collections and objects from the top down and write their values to a spreadsheet and then loop through the spreadsheet rows and output those to a delimited text file. Then do the reverse to read the text file back in - from text to spreadsheet, then from spreadsheet into collections and objects.

Maybe the other option would be to bypass the spreadsheet and use a TextStream? Would that be possible and would it be the way to go if I'm planning on converting this VBA application to standalone VB.NET sometime in the future?

解决方案

One good option is to store your collections as XML files. It will be relatively easy to add or read individual elements afterwards; and you'll save yourself from the indexing nightmare that will inevitably occur if you try to arrange your 4+ dimensional data onto a two-dimensional spreadsheet. They're text files, so human readable; if file size becomes a problem, just zip them.

You'll just have to devise an appropriate format, i.e. tag names etc., see example at the end of this answer.

EDIT: If having the data packaged into your Excel workbook is really important to you, then you can just slap the xml code onto one of the sheets and store it there... I've done this before.

Create an XML document as shown below:

Set xmlFatAssCollectionA = CreateObject("Msxml2.DOMDocument.6.0")

Then edit it: loop through your collections, appending each element as a child of the larger collection, as appropriate. When done, save the XML document.

xmlFatAssCollectionA.save("C:\MyDir\MyCollection.xml")

Documentation: http://msdn.microsoft.com/en-us/library/ms756987%28v=VS.85%29.aspx

You can use XPath to access the required nodes/elements. XPath tutorial

Here's what your resulting xml file might look like. I only added one collectionB object, and omitted the whole collectionA layer, but you get the idea.

<?xml version="1.0" encoding="ISO-8859-1"?>

<bObject>
  <cObject>
    <contentObject>
      <title lang="da">Kejserens nye klæder</title>
      <author>H.C. Andersen</author>
      <price>priceless</price>
    </contentObject>
  </cObject>
  <cObject>
     <contentObject>
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </contentObject>
    <contentObject>
      <title lang="fr">Les Schtroumpfs</title>
      <author>Some person</author>
      <year>1985</year>
    </contentObject>
  </cObject>
</bObject>

这篇关于将我的集合和对象写入磁盘并在以后重新导入的最佳策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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