简体   繁体   中英

Should I use Dictionary instead of XmlDocument?

I have an XML file which the (asp.net) application loads on startup, and every http request requests at least 50 items from it, by calling GetElementById. The class which handles it is static, so that XmlDocument.Load() is called just once. The XML file structure is very simple, where every node has only "ID" tag, and there is no hierarchy between the nodes (all of them are under the root node). A typical node is of the format:

<txt id="OfficeEmail">bla@bla.com</txt>

I was wondering if loading the XML content (~300 nodes) to a static dictionary will improve the performance when requesting for content. I can guess that once the XmlDocument is loaded, it caches the nodes in some efficient data structure, but could it be that there is a huge overhead over implement it with a dictionary?

300 Nodes should hardly be a problem. but if every request makes 50 calls to the xmldocument, then you should measure the access times. and if it doesn't meet your bar, you could do:

Dictionary<string, string> values = new Dictionary<string, string>();
// read the xml file on start up populate the dictionary.

// after that, every Dictionary access will be O(1)
string value = values["OfficeEmail"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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