There is a code that accesses the xml file, extracts data from it into the f01x array, and processes this array. The code is tied to Excel via DNA. A function has been created that contains a number of arguments. The function works fine, but very slowly. The problem is that if you need to use the function 1500 times (in different cells and with different periodicity), then a huge part of the time (cumulatively 30+ minutes) takes exactly the connection to the xml file (on the C drive).
The idea that came to my mind is to once read the xml file into an array and leave this f01x array in RAM, and later, when the same function is started, they are in another Excel cell (say, 5 minutes after the first launch) , check if the function's arguments are equal to some cells in the array, then we get the f01x array from the PC's RAM (if the cleaner does not overwrite it), bypassing reading from the C: \ 01.xml file into the array, and if the function arguments are not equal to some cells of the array , then the array f01x is filled from the xml file.
What I would like to implement should look like this:
- We get all the arguments to the function.
- Check if there is an array of f01x in RAM.
- If the argument of the function "Date" is equal to a certain cell of the array, then we take an array from RAM, otherwise, we fill the array from the xml file.
Initially, I want to deal with RAM, and in the case where the array is there, take it from RAM, if it is not there, read it from xml. Later, I will finish the comparison of arguments.
Tell me, please, how to do this? Maybe someone has similar examples, please share. It is necessary that the f01X array remains at the output (filled either from the file, if the first function call occurs, or from RAM, if the function was already called before (5-10-20 minutes ago)). Help me please.
After some discussions, the following simple code was born:
namespace C_Sharp { public class Ground { // Создаю приватное поле в классе Ground private string[,] f01x; // Создаю конструктор класса с параметрами и в конструкторе заполняю поле из файла public Ground(DateTime data, string ekv) { XmlDataDocument xmldoc = new XmlDataDocument(); XmlNodeList xmlnode; FileStream fs = new FileStream(@"C:\01.xml", FileMode.Open, FileAccess.Read); xmldoc.Load(fs); xmlnode = xmldoc.GetElementsByTagName("DATA"); string[,] f01x = new string[xmlnode.Count, 11]; f01x[2, 5] = xmlnode[2].ChildNodes.Item(6).InnerText.Trim(); } [ExcelFunction(Description = "Hello", IsVolatile = true)] public static string func01( [ExcelArgument(Name = "Дата", Description = "День месяца")] DateTime data, [ExcelArgument(Name = "Капитал", Description = "Эквити")] string ekv) { // !!!! Так ругается компилятор // !!!! Для нестатического поля, метода или свойства требуется ссылка на объект return f01x[2, 5]; } } }
f01X
welcoming field of the class and check if it is not initialized - then read it from the file, if initialized - then do nothing. - tym32167