Asle Frantzen

Create C# classes from OSIsoft.AF.xsd

Blog Post created by Asle Frantzen Champion on Apr 26, 2011

In my first blog post I presented updated performance results for the case I presented at vCampus Live! 2010. One of the things I briefly talk about in my presentation, is that I use an XSD Tool to create C# classes from an XSD file which comes along with AF. Today I would like to go through this process step by step.

 

The file OSIsoft.AF.xsd is found in the AF folder, which is located in the PIPC folder.

 

The tool is called XSD.exe and is found in the folder C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin - at least on my computer. It's very easy to use, and there are lots of options you can turn on. The default language is C# so we don't have to specify that, but you can use VB.net and other languages as well. Type xsd.exe /? to get a complete list of options.

 


The command I've used is:

 

xsd.exe OSIsoft.AF.xsd /Classes /namespace:Amitec.AF_Import

 

and this will generate a .cs file which you can import into your Visual Studio solution. The /Classes flag must be present, and I've also specified the namespace of my C# project.

 

1425.xsdtool_5F00_cmd.jpg

 


I've imported both the .xsd and the .cs file into my Visual Studio solution, here are a couple of screenshots:

7875.vs_5F00_OSIsoft_5F00_AF_5F00_xsd_5F00_small.jpg 5756.vs_5F00_OSIsoft_5F00_AF_5F00_cs_5F00_small.jpg
OSIsoft.AF.xsd OSIsoft_AF.cs

 

 

Now we can start using the classes in the .cs file. This will be an "AF disconnected way" of dealing with a data hierarchy - as it doesn't connect to AF in any way, as opposed to using the AF SDK. You could start with something like this:

 
AF m_AFInstance = new AF();        
    
AFElement myAFElement = new AFElement();
myAFElement.Name = "FirstElement";
myAFElement.Description = "My first AF Element";

m_AFInstance.Items[0] = myAFElement;

 

 

After completing the business logic for your application you can do like I did, serialize your AF instance to XML - so that you can import it through PI System Explorer:

 
System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(AF));
TextWriter writer = new StreamWriter("AF_Import_"+DateTime.Now.ToString("ddMMyyyy_HHmmsss")+".xml");
xSerializer.Serialize(writer, m_AFInstance);

 

Outcomes