private static void serializeObject(string filename, part newpart) { // Create a datetime format object DateTimeFormatInfo dateFormat = new CultureInfo("en-US").DateTimeFormat; try { //create an eConnect schema object IVItemMasterType iv = new IVItemMasterType(); taUpdateCreateItemRcd newItem = new taUpdateCreateItemRcd(); newItem.ITEMNMBR = newpart.itemNumber; //Item number newItem.ITEMDESC = newpart.description; //Item description newItem.ITMSHNAM = "temp"; //short desc newItem.ITMGEDSC = "temp"; //general desc newItem.UseItemClass = 1; newItem.ITMCLSCD = newpart.category; //Part or Assembly newItem.ITEMTYPESpecified = true; //Say custom itemtype is being used newItem.ITEMTYPE = 1; //Sales item newItem.UOMSCHDL = newpart.units; newItem.DECPLCURSpecified = true; //Say a custom decplcur is being used newItem.DECPLCUR = 3; //Needed to make item in IVR10015 newItem.NOTETEXT = newpart.purchasing; newItem.UpdateIfExists = 0; //Populate schema object with newItem info iv.taUpdateCreateItemRcd = newItem; // Create an array that holds ItemMasterType objects // Populate the array with the ItemMasterType schema object IVItemMasterType[] myItemType = { iv }; // Create an eConnect XML document object and populate it // with the ItemMasterType schema object eConnectType eConnect = new eConnectType(); eConnect.IVItemMasterType = myItemType; // Create a file to hold the serialized eConnect XML document FileStream fs = new FileStream(filename, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); // Serialize the eConnect document object to the file using the XmlTextWriter. XmlSerializer serializer = new XmlSerializer(eConnect.GetType()); serializer.Serialize(writer, eConnect); writer.Close(); } //If an eConnect exception occurs, notify the user catch (eConnectException ex) { Console.Write(ex.ToString()); } }
/// <summary> /// serialize item /// </summary> /// <param name="filename"></param> public static void SerializeItemObject(string filename) { try { // Instantiate an eConnectType schema object eConnectType eConnect = new eConnectType(); // Instantiate a IVItemMasterType schema object IVItemMasterType itemType = new IVItemMasterType(); // Instantiate a taUpdateCreateItemRcd XML node object taUpdateCreateItemRcd item = new taUpdateCreateItemRcd(); // Create an XML serializer object XmlSerializer serializer = new XmlSerializer(eConnect.GetType()); // Populate elements of the taUpdateCreateVendorRcd XML node object //item.CUSTNMBR = "Customer001"; //item.CUSTNAME = "Customer 1"; //item.ADDRESS1 = "2002 60th St SW"; //item.ADRSCODE = "Primary"; //item.CITY = "NewCity"; //item.ZIPCODE = "52302"; // Populate the IVItemMasterType schema with the taUpdateCreateItemRcd XML node itemType.taUpdateCreateItemRcd = item; IVItemMasterType[] itemMaster = { itemType }; // Populate the eConnectType object with the IVItemMasterType schema object eConnect.IVItemMasterType = itemMaster; // Create objects to create file and write the customer XML to the file FileStream fs = new FileStream(filename, FileMode.Create); XmlTextWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); // Serialize the eConnectType object to a file using the XmlTextWriter. serializer.Serialize(writer, eConnect); writer.Close(); } // catch any errors that occur and display them to the console catch (System.Exception ex) { Console.Write(ex.ToString()); } }
/// <summary> /// save part vendor master /// </summary> /// <param name="part"></param> /// <returns></returns> public OperationResult SavePartVendorMaster(IV00103_Part_Vendor_Master part) { var operationResult = new OperationResult(); var existingPart = _dynamicsContext.IV00103_Part_Vendor_Master.FirstOrDefault(x => x.ITEMNMBR.Replace(" ", string.Empty).ToLower() == part.ITEMNMBR.Replace(" ", string.Empty).ToLower()); if (existingPart == null) { logger.Debug("Vendor Part is being created..."); using (eConnectMethods e = new eConnectMethods()) { try { // Instantiate a taCreateItemVendors_ItemsTaCreateItemVendors XML node object taCreateItemVendors_ItemsTaCreateItemVendors item = new taCreateItemVendors_ItemsTaCreateItemVendors(); //Populate elements of the taCreateItemVendors_ItemsTaCreateItemVendors XML node object item.ITEMNMBR = part.ITEMNMBR; item.VENDORID = part.VENDORID; item.VNDITNUM = part.VNDITNUM; item.UpdateIfExists = 0; // Instantiate a IVItemMasterType schema object IVItemMasterType itemtype = new IVItemMasterType(); // Populate the IVItemMasterType schema with the taCreateItemVendors_ItemsTaCreateItemVendors XML node itemtype.taCreateItemVendors_Items = new taCreateItemVendors_ItemsTaCreateItemVendors[1] { item }; IVItemMasterType[] vendorItem = { itemtype }; // Instantiate an eConnectType schema object eConnectType eConnect = new eConnectType(); // Instantiate a Memory Stream object MemoryStream memoryStream = new MemoryStream(); // Create an XML serializer object XmlSerializer serializer = new XmlSerializer(eConnect.GetType()); // Populate the eConnectType object with the IVItemMasterType schema object eConnect.IVItemMasterType = vendorItem; // Serialize the eConnectType. serializer.Serialize(memoryStream, eConnect); // Reset the position of the memory stream to the start. memoryStream.Position = 0; // Create an XmlDocument from the serialized eConnectType in memory. XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(memoryStream); memoryStream.Close(); // Call eConnect to process the XmlDocument. e.CreateEntity(_dynamicsConnection, xmlDocument.OuterXml); operationResult.Success = true; operationResult.Message = "Success"; } // The eConnectException class will catch eConnect business logic errors. // display the error message on the console catch (eConnectException exc) { Console.Write(exc.ToString()); operationResult.Success = false; operationResult.Message = "Error"; logger.ErrorFormat("Error saving new part price header: {0} ", exc.ToString()); } // Catch any system error that might occurr. // display the error message on the console catch (System.Exception ex) { Console.Write(ex.ToString()); operationResult.Success = false; operationResult.Message = "Error"; logger.ErrorFormat("Error saving new part price header: {0} ", ex.ToString()); } finally { // Call the Dispose method to release the resources // of the eConnectMethds object e.Dispose(); } } // end of using statement } else { operationResult.Success = false; operationResult.Message = "Duplicate Entry"; } return(operationResult); }