Loading Element Hierarchy
- Last UpdatedJan 12, 2026
- 2 minute read
- PI System
- AF SDK 3.2.0
- Developer
Because elements are organized into a hierarchy, there are frequently situations where an application is going to want an entire hierarchy loaded into the client memory space. This example uses the LoadElementsToDepth method to illustrate a mechanism for bulk loading an entire hierarchy of elements into the client.
1// Get the Database 2PISystems myPISystems = new PISystems(); 3PISystem myPISystem = myPISystems.DefaultPISystem; 4if (myPISystem == null) 5 throw new InvalidOperationException("Default PISystem was not found."); 6AFDatabase myDB = myPISystem.Databases[dbName]; 7if (myDB == null) 8 throw new InvalidOperationException("Database was not found."); 9 10// Get a specific area of the element hierarchy to load 11AFElement myNorthPlantArea56 = myDB.Elements[@"NorthPlant\Area56"]; 12if (myNorthPlantArea56 == null) 13 throw new InvalidOperationException(@"Element 'NorthPlant\Area56' was not found."); 14 15// Use AFElement.LoadToDepth to fully load this part of the hierarchy. 16// This will utilize fewer RPC's than navigating through the hierarchy 17// one element at a time. Note that care needs to be taken to not attempt 18// to load too many objects into memory at once. Here, we limit the load 19// to 3 additional levels of the hierarchy, not to exceed 1000 total elements. 20AFNamedCollectionList<AFElement> allElements = AFElement.LoadElementsToDepth( 21 myNorthPlantArea56.Elements, true, 3, 1000); 22 23// Once the elements are loaded, we can process them as usual. In the example 24// below, we have written a recursive routine which counts the bad data for the 25// entire element hierarchy. 26int badDataCount = CountBadData(myNorthPlantArea56, 4); 27 28Console.WriteLine("Found {0} Elements which contain {1} Attributes with bad data.", 29 allElements.Count, badDataCount); 30 31// Prevent garbage collection by the .NET CLR until this point. 32GC.KeepAlive(allElements);