Skip to main content
PI System
Asset Framework
XML Management

Exporting Partial AF Databases into Well-Formed XML: Strategies and Best Practices

Explore strategies for exporting part of an AF database into a single, well-formed XML file, ensuring integrity and ease of re-import in PI System.

Roshan Soni

4 min read

Exporting Partial AF Databases into Well-Formed XML: Strategies and Best Practices

In the realm of managing Asset Framework (AF) databases within the PI System, one common task is exporting data. However, when it comes to creating partial exports, challenges often arise due to the structure of XML and the requirements for maintaining a well-formed document. In this blog post, we'll explore strategies for exporting partial AF data, focusing specifically on how to combine multiple XML exports into a single well-structured document.

The Need for Partial Exports

Exporting an entire AF database isn't always necessary or efficient. Users may want to focus on specific elements, such as certain templates, categories, or a selected group of elements, which makes partial exports valuable. The challenge lies in ensuring these partial exports are integrated into a well-formed XML document suitable for re-import or further processing.

Common Issues in XML Export

When using standard export functions, you might find that each ExportXML call generates its own root element, which leads to multiple root elements in a single document�an invalid XML structure. This structure complicates any future attempts to re-import or process the XML file.

Proposed Solutions

1. Utilizing PIExportMode.AllReferences

The simplest way to solve export issues is to leverage the PIExportMode.AllReferences mode. This approach involves making a single ExportXML call that includes all necessary elements, templates, categories, and references. This way, you encapsulate all interrelated data in one go, reducing redundancy and ensuring consistency across elements.

Here's a snippet illustrating this approach:

var export = new StringBuilder();
export.Append(piSystem.ExportXML(database.Elements, PIExportMode.AllReferences));
File.WriteAllText("partial_export.xml", export.ToString());

2. Merging XML Documents Programmatically

For scenarios where exporting everything in one go is not feasible, you can export individual components and merge the resulting XML structures programmatically. Using System.Xml.Linq, you can load and combine XML documents, ensuring you maintain a single root element.

Code Example:

var xml1 = XDocument.Load("file1.xml");
var xml2 = XDocument.Load("file2.xml");

// Create a new root to avoid multiple root elements
var combinedDoc = new XDocument(new XElement("AFDatabase"));

combinedDoc.Root.Add(xml1.Root.Elements());
combinedDoc.Root.Add(xml2.Root.Elements());

combinedDoc.Save("combined_export.xml");

This approach ensures you can export individual segments and then combine them into a single XML file, effectively circumventing the issue of multiple root elements.

Conclusion

Exporting parts of an AF database without compromising the structure of your XML can be achieved through careful planning and execution. Whether you use the PIExportMode.AllReferences for a more integrated export or merge separate exports, what's important is maintaining a coherent and well-formed XML that fully represents your desired data slices. This effort ensures smooth transitions for re-imports or further manipulation.

By adopting these strategies, you can achieve flexibility in managing and migrating AF data without encountering the common pitfalls associated with partial exports.

Tags

#PI System
#AFSDK
#XML
#Data Export

About Roshan Soni

Expert in PI System implementation, industrial automation, and data management. Passionate about helping organizations maximize the value of their process data through innovative solutions and best practices.

Sign in to comment

Join the conversation by signing in to your account.

Comments (0)

No comments yet

Be the first to share your thoughts on this article.

Related Articles

Enhancing PI ProcessBook Trends with Banding and Zones: User Needs, Workarounds, and the Road Ahead

A look at the user demand for trend banding/zoning in OSIsoft PI ProcessBook, current VBA workarounds, UI challenges, and how future PI Vision releases aim to address these visualization needs.

Roshan Soni

Migrating PIAdvCalcFilVal Uptime Calculations from PI DataLink to PI OLEDB

Learn how to translate PI DataLink's PIAdvCalcFilVal advanced calculations—like counting uptime based on conditions—into efficient PI OLEDB SQL queries. Explore three practical approaches using PIAVG, PIINTERP, and PICOunt tables, and get tips for validation and accuracy.

Roshan Soni

Understanding PI Web API WebID Encoding: Can You Generate WebIDs Client-Side?

Curious about how PI Web API generates WebIDs and whether you can encode them client-side using GUIDs or paths? This article explores the encoding mechanisms, current documentation, and best practices for handling WebIDs in your applications.

Roshan Soni