CSharp Examples

From DevWiki

Revision as of 18:25, 21 January 2011 by Vrmb (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

>== Volusion API Reader ==


Volusion API Reader is an example program created in C# in order to show how to utilize the import / export capabilities of the API.

This is a complete visual studio solution and includes complete source code and binaries. The program consists of two parts:

Export:
Read from the Volusion API and display the data or continue to export it into another format or custom application.

Import:
Import data from an external program into your Volusion store.

Data Format:
Volusion API uses XML based format to import and export data. The XSD (XML Schema Definition) files can be found on this WIKI. In order to quickly load data from and into the Volusion server, we have converted the XSD file into a CS file using xsd.exe. More information on this tool can be found here: xsd.exe on MSDN

Reading Data
Reading data from The API can be done by simply loading the XML into a document:

XmlDocument APIDoc = null;
XmlTextReader reader = new XmlTextReader(APIURL);
APIDoc = new XmlDocument();
APIDoc.Load(reader);
reader.Close();


Writing data
In order to write data into the API we are utilizing the objects we built using the provided XSD files.

xmldata data = new xmldata();
data.Items = new xmldataProducts[1];
data.Items[0] = new xmldataProducts();
data.Items[0].ProductName = productName;
data.Items[0].ProductCode = productCode;
data.Items[0].ProductPrice = productPrice;

Since the Volusion API expects an XML formatted message we need to turn the object into XML

StringBuilder productSb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(productSb);
XmlSerializer serializer = new XmlSerializer(typeof(DataObjects.xmldata));
serializer.Serialize(writer, product);

Then post the data into our server

HttpWebRequest APIRequest = (HttpWebRequest)WebRequest.Create(APIURL);
APIRequest.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
APIRequest.ContentType = "application/x-www-form-urlencoded";
APIRequest.ContentLength = data.Length;

using (Stream APIStream = APIRequest.GetRequestStream())
{
    APIStream.Write(data, 0, data.Length);
}

How to run the program:
1. Opent VAPIReader.sln using visual studio 2005
2. Before running the solusion you will need to configurate your store URL, user name and encrypted password.
In your visual studio, use the solusion explorer to navigate to VAPIReader/app.config
Modify the values

<add key="VOLUSION_BASE_API" value="http://www.yourdomain.com/net/WebService.aspx"/>
<add key="VOLUSION_USER" value="YourLogin"/>
<add key="VOLUSION_PASSWORD" value="YourEncryptedPassword"/>

To match the values in your account.
3. Press F5 in order to run the program.

Download Solution file here: VAPIReader.zip

Volusion API Format Flex

Volusion API Format Flex is an example program created in C# in order to show how to utilize the export capabilities of the API to different XML structure.
This is a complete visual studio solution and includes complete source code and binaries.

The concept behind this program is to simplify the modification of exported files. It includes a working example converting Volusion orders data into UPS data to be used for shipping orders.

It includes a blank UPS XML file and a Map of replacement values. The map indicates which values in the Volusion order will be stored in a UPS shipping order.

Exporting data from the API

Is done by reading the orders API and storing the data as in memory XML

XmlTextReader reader = new XmlTextReader(APIURL);
APIDoc = new XmlDocument();
APIDoc.Load(reader);
reader.Close();

Then load the UPS map or any custom map we have added to Mapper.xml

Mapper mapper = new Mapper(mapName);

once we have our order we will load a blank UPS order and simply fill in the values from our Volusion order.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(destinationDoc.NameTable);
sourceNode.SelectSingleNode(mapper.TagDictionary[key], nsmgr).InnerText = destinationNode.SelectSingleNode(key).InnerText;

This is a very generic replace method so Any special replacment values will have to be added for every order.

loadTo.SelectSingleNode("//ups:Attention", nsmgr).InnerText = loadFrom.SelectSingleNode("ShipFirstName").InnerText +
" " + loadFrom.SelectSingleNode("ShipLastName").InnerText;

Finaly we can output the result into a file in order to load it to UPS

fileStream = mainSaveFileDialog.OpenFile();
ASCIIEncoding encoder = new ASCIIEncoding();
xmlData = GetFormatedXML(stateInfo.ToString());
byte[] data = encoder.GetBytes(xmlData);
fileStream.Write(data, 0, data.Length);
fileStream.Close();

Running the program

Open the file VAPIShape.sln using visual studio 2005.
1. Press F5 to sun the solution. A settings screen will show up, prompting you to enter your store’s settings.
2. Enter your API URL, user name, encrypted password and press save.
3. In the main window, press the Export button in order to select a file to save the data to.
4. Press ‘save’ on the dialog in order to start the exporting process.

The program will connect to the Volusion API server and retrieve all of your new orders.
The data will be saved in UPS format into the selected file.

Download Solution file here: VAPIShape.zip

Navigation