简体   繁体   English

我应该使用什么来临时存储将被重用的XML数据?

[英]What should I use to temporary store XML data that will be reused?

I have a 47mb XML file that I have to read over and over and over on my application but mostly it only wants 1 node defined by a given term every now and then. 我有一个47mb的 XML文件,我必须在我的应用程序上反复阅读,但大多数情况下它只需要一个给定术语定义的1个节点。

Example: 例:

<customers>
    <customer>
        <id>someid</id>
        <name>somename</name>
    </customer>
    <customer>
        <id>someid</id>
        <name>somename</name>
    </customer>

    ...

    <customer>
        <id>someid</id>
        <name>somename</name>
    </customer>
    <customer>
        <id>someid</id>
        <name>somename</name>
    </customer>
</customers>

So I was thinking on loading the xml to a Dictionary or List once the programs load up and keep it there to reuse until the application is closed. 所以我想在程序加载后将xml加载到Dictionary或List中,并将其保留在那里,直到应用程序关闭为止。

  • What I wanted to know what would be the best type of stored for it ? 我想知道什么是最好的存储类型?

If I have to use a dictionary for example: 如果我必须使用字典例如:

private Dictionary<int,string> myCustomerList = new Dictionary<int,string>();

which I could use later to get the information I need like: 我以后可以使用它来获取我需要的信息:

if (myCustomerList.ContainsKey(keyX))
    myValue = myCustomerList[keyX];
  • Is this a bad way to go or what should I be looking at ? 这是一个不好的方式,或者我应该看什么?

There is nothing functionally wrong with your approach, but what happens when more information is added to the Customer? 您的方法没有任何功能上的错误,但是当向客户添加更多信息时会发生什么? For example, you might have a "location" element: 例如,您可能有一个“location”元素:

   <customer>
        <id>someid</id>
        <name>somename</name>
        <location>somename</location>
    </customer>

Also consider the scenario where you move your data to a database - you will have to rewrite a lot of code. 还要考虑将数据移动到数据库的场景 - 您必须重写大量代码。

It is generally considered bad practice to work directly with XML objects through the duration of your application. 在应用程序的持续时间内直接使用XML对象通常被认为是不好的做法。 The "correct" approach is to first load your XML data into Object Oriented objects. “正确”的方法是首先将XML数据加载到面向对象的对象中。

You should create a class named Customer , and use List<Customer> customers =new List<Customer>() to store the objects. 您应该创建一个名为Customer的类,并使用List<Customer> customers =new List<Customer>()来存储对象。 You can then use customers.Find(x=>x.Id == myId) to locate your objects. 然后,您可以使用customers.Find(x=>x.Id == myId)来定位您的对象。

Remember that if you store your objects in memory, your application will use at least 47mb of RAM. 请记住,如果将对象存储在内存中,则应用程序将使用至少47mb的RAM。 This could potentially grow as your business attains more customers. 随着您的业务获得更多客户,这可能会增长。 Perhaps you should look at lazy-loading your customers (retrieving them from the XML file when they are required) - this will be slower so it really depends on your throughput and volume requirements. 也许您应该考虑延迟加载客户(在需要时从XML文件中检索它们) - 这将会更慢,因此它实际上取决于您的吞吐量和数量要求。

Your approch is quite right. 你的approch非常正确。 if in future when more information is added to customer class then use list generic class otherwise your approch is right. 如果以后将更多信息添加到客户类,则使用list generic class,否则您的approch是正确的。

List - one of my favs - can be used with generics, so you can have a strongly typed array, eg List. 列表 - 我最喜欢的一个 - 可以与泛型一起使用,因此您可以使用强类型数组,例如List。 Other than that, acts very much like ArrayList. 除此之外,其行为与ArrayList非常相似。

Dictionary - same as above only strongly typed via generics, such as Dictionary 字典 - 与上面相同,仅通过泛型强类型,例如Dictionary

use List and Dictionary all the time. 一直使用List和Dictionary。

i would say 47 mb of a file, loading all at once in a datatable would be very easy to do and also good for searching. 我会说47 MB​​的文件,在数据表中一次加载将非常容易,也有利于搜索。

You should consider WeakReferences to store this datatable in memory. 您应该考虑使用WeakReferences将此数据表存储在内存中。

47MB is fairly large data for an app to keep in memory all the time. 47MB是一个相当大的数据,应用程序始终保持在内存中。

However, you can combine XmlReader with XNode.ReadFrom to read large Xmls with less memory. 但是,您可以将XmlReaderXNode.ReadFrom结合使用,以读取内存较少的大型Xml。

You can use this method to write a method that returns a collection of nodes, yielding each node as the node is read from the reader. 您可以使用此方法编写一个返回节点集合的方法,从读取器读取节点时生成每个节点。 This method enables you to process arbitrarily large XML files with a very small memory footprint. 此方法使您可以使用非常小的内存占用来处理任意大型XML文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM