Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Introduction

Handling matrixes is a common action done by add-ons.

In previous versions (6.5, 2004 & 2005) reading values from system matrixes could be done cell by cell in the GUI level or by reading from DataSource objects. This is Slow. And can result with performance issues.

The SerializeAsXML function returns an xml string containing all the matrix data. It saves time and improves the performance dramatically.

Another benefit of this function is that the XML that is returned from this function is compatible with .Net objects, and can be used easily in .Net projects.

Note: The SerializeAsXML is available only for reading values from matrix!

Before reading the blog, download the code samples from here.

How to develop using the SerializeAsXML method

1. First generate the .Net matrix class (based on SAP Business One matrix UI object). Generating the class includes 2 steps:

    a. Generate the Schema xml file.

    b. Generate .NET matrix class.

2. Second, use the .Net matrix class to read the UI matrix data

    a. Import the class to your Add-On.

    b. Read UI matrix data using XML string.

    c. Read UI matrix data using XML file.

 

1.a Generate the .Net matrix class - Generage matrix schema xml file

Use the ‘Matrix.GetSchema()‘ method to get the matrix schema, and save it to a file. (Sample code shown below).

This method should be done only once. The Schema data is the same for every UI matrix! (Every time you'll run this code, the result will be the same, regardless the form and the matrix you'll be getting the data from).

  • Run the "MatrixSerializAsXML" sample.
  • Load the "Sales Order" form (Sales order form will be loaded with a new button: "Generate Matrix Schema" on the right to the "Cancel" button")

 

Click on the button and the schema file will be generated.

 

In the code sample inside the ‘ItemEvent' method, we catch the click event on the "Generate Matrix Scehma" button. Once catching the event we can generate the xml schema for the matrix UI object as shown below:

 

SAPbouiCOM.Form oForm;

SAPbouiCOM.Item oMatItem;

SAPbouiCOM.Matrix oMatrix;

string sMatrixSchemaStr, sFileName;

System.Xml.XmlDocument SchemaXmlDoc = new System.Xml.XmlDocument();

sFileName = System.AppDomain.CurrentDomain.BaseDirectory;

sFileName = sFileName + "SchemaXML.xsd";

 

oForm = SBO_Application.Forms.Item(FormUID);

oMatItem = oForm.Items.Item("38");

oMatrix = (SAPbouiCOM.Matrix)oMatItem.Specific;

 

sMatrixSchemaStr = oMatrix.GetSchema();

           

SchemaXmlDoc.LoadXml(sMatrixSchemaStr);

SchemaXmlDoc.Save(sFileName);

** The schema for the UI matrix will be saved under the ‘ inDebug' folder.

 

 

1.b Generate the .Net matrix class - Generate .NET matrix class

Use the Visual Studio XML Schema Definition Tool (Xsd.exe) in order to generate runtime classes from an XSD schema file (The generated classes can be used in conjunction with System.XML.Serialization.XMLSerializer to read and write XML code that follows the schema.)

* The Sxd.exe is a tool provided with visual studio, in order to use it: search for Sxd.exe file.

** You can take the matrix.cs file from the provided sample as is (since we have the same schema for all UI matrixes)

In the cmd window type: xsd.exe_full_path /c MatrixSchema.xsd_file_full_ path

For example, if both relevant files are saved under the C:SerializeAsXml folder, the command for generating C# class should look like this:

And this is the output you should get:

 

The new class was generated and saved as: C:MatrixSchema.cs

 

2.a Use the .Net Matrix class - Import the class to your Add-On

Import the generated class ‘MatrixSchema.cs' to your .NET Add-On solution.

We'll use the class for reading the UI matrix data quickly. This can be done either using XML string or using XML file.

2.b Use the .Net Matrix class - Read UI matrix data using XML string

  • Run the "MatrixSerializAsXML" sample.
  • Load the "Sales Order" form
  • Load an existing sales order and update it -> The "et_FORM_DATA_UPDATE" event will be fired.
  • In the "FormDataEvent" method update event, we use the SerializeAsXML for reading the UI matrix data:
 

string sMatrix;

SAPbouiCOM.Form oForm;

SAPbouiCOM.Matrix oMatrix;

StringReader StrReader;

Matrix netMatrix;

string XMLFileName;

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.Serialization.XmlSerializer XmlSerializer = new  

System.Xml.Serialization.XmlSerializer(typeof(Matrix));

oForm = SBO_Application.Forms.ActiveForm;

oMatrix = (SAPbouiCOM.Matrix)oForm.Items.Item(38).Specific;

sMatrix = oMatrix.SerializeAsXML(SAPbouiCOM.BoMatrixXmlSelect.mxs_All);

// Load Matrix to .Net matrix object //     

StrReader = new StringReader(sMatrix);

// Call the Deserialize method and cast to the object type

netMatrix = (Matrix)XmlSerializer.Deserialize(StrReader);

// Sample of reading the data from the generated .NET matrix

string str;

str = netMatrix.Rows[0].Columns[3].Value.ToString ();

str = "Item Code of First Line is: " + str;

SBO_Application.MessageBox (str,1,"OK","Cancel","") ;

   

2.c Use the .Net Matrix class - Read UI matrix data using XML File

  • Run the "MatrixSerializAsXML" sample.
  • Load the "Sales Order" form
  • Add a new sales order -> The "et_FORM_DATA_ADD" event will be fired. The code sample is in the "FormDataEvent" method
  • In the "FormDataEvent" method add event, we use the SerializeAsXML for reading the UI matrix data:
 

string sMatrix;

SAPbouiCOM.Form oForm;

SAPbouiCOM.Matrix oMatrix;

StringReader StrReader;

Matrix netMatrix;

string XMLFileName;

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.Serialization.XmlSerializer XmlSerializer = new  

System.Xml.Serialization.XmlSerializer(typeof(Matrix));

oForm = SBO_Application.Forms.ActiveForm;

oMatrix = (SAPbouiCOM.Matrix)oForm.Items.Item(38).Specific;

sMatrix = oMatrix.SerializeAsXML(SAPbouiCOM.BoMatrixXmlSelect.mxs_All);

XMLFileName = System.AppDomain.CurrentDomain.BaseDirectory;

XMLFileName = XMLFileName + "Matrix.xml";

xmlDoc.LoadXml(sMatrix);

xmlDoc.Save(XMLFileName);

// Load Matrix from XML file to .Net matrix object //

System.IO.FileStream myFileStream =

      new System.IO.FileStream(XMLFileName, System.IO.FileMode.Open);

netMatrix = (Matrix)XmlSerializer.Deserialize(myFileStream);

string str;

str = netMatrix.Rows[0].Columns[3].Value.ToString();

str = "Item Code of First Line is: " + str;

SBO_Application.MessageBox(str, 1, "OK", "Cancel", "");

 

 

 

 

                                                                                                                                                                                                                                                                                                                                                                                                                                          

 

                                                                                                                                                                                                                                                          
1 Comment