public static CarFaxViewModel ConvertXmlToCarFaxModelAndSave(string Vin, string carFaxUsername, string carFaxPassword) { var carfax = GetCarFaxReportInDatabase(Vin); if (carfax.ExistDatabase == 1) { carfax.Success = true; return(carfax); } else { var doc = MakeApiCall(Vin, carFaxUsername, carFaxPassword); var root = doc["carfax-response"]; carfax.ReportList = new List <CarFaxWindowSticker>(); if (!String.IsNullOrEmpty(root.InnerText)) { if (root["vin-info"] != null) { try { carfax.Vin = root["vin-info"]["vin"].InnerText; carfax.NumberofOwners = root["vin-info"]["number-of-owners-indicator"]["number-of-owners-indicator-value"].InnerText; carfax.ServiceRecords = root["vin-info"]["number-of-service-records-indicator"].InnerText; carfax.AccidentCountsXml = root["vin-info"]["accident-count"].InnerText; carfax.AccidentIndicator = root["vin-info"]["accident-indicator"].InnerText.Equals("N") ? false : true; carfax.FrameDamageIndicator = root["vin-info"]["frame-damage-indicator"]["frame-damage-value"].InnerText.Equals("N") ? false : true; carfax.BrandedTitleIndicator = root["vin-info"]["branded-title-indicator"]["branded-title-indicator-value"].InnerText.Equals("N") ? false : true; carfax.BuyBackGuarantee = root["vin-info"]["bbg-indicator"]["bbg-indicator-value"].InnerText.Equals("N") ? false : true; carfax.MajorProblemIndicator = root["vin-info"]["major-problem-indicator"]["major-problem-value"].InnerText.Equals("N") ? false : true; carfax.DamageIndicator = root["vin-info"]["damage-indicator"]["damage-indicator-value"].InnerText.Equals("N") ? false : true; carfax.PriorRental = false; if (root["vin-info"] != null) { var content = root["vin-info"]["types-of-use"]; if (content != null) { for (int i = 0; i < content.ChildNodes.Count; i++) { if (content.ChildNodes[i].InnerText.Equals("Rental")) { carfax.PriorRental = true; break; } } } } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["AccidentIndicators"]["AccidentIndicatorsText"].InnerText)) { string imageURL = root["vin-info"]["WindowSticker"]["AccidentIndicators"]["CheckmarkImage"].InnerText.Replace("_bw", ""); var tmp = new CarFaxWindowSticker() { Text = root["vin-info"]["WindowSticker"]["AccidentIndicators"]["AccidentIndicatorsText"].InnerText, Image = imageURL }; carfax.ReportList.Add(tmp); } int result; if (int.TryParse(carfax.AccidentCountsXml, out result) && result > 0) { carfax.AccidentCounts = result; } if (carfax.PriorRental) { var context = new whitmanenterprisewarehouseEntities(); var cars = context.whitmanenterprisedealershipinventories.Where(i => i.VINNumber.Equals(Vin)); foreach (var item in cars) { item.PriorRental = true; } if (cars.Any()) { context.SaveChanges(); } } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["AirbagDeployment"]["AirbagDeploymentText"].InnerText)) { string imageURL = root["vin-info"]["WindowSticker"]["AirbagDeployment"]["CheckmarkImage"].InnerText.Replace("_bw", ""); var tmp = new CarFaxWindowSticker() { Text = root["vin-info"]["WindowSticker"]["AirbagDeployment"]["AirbagDeploymentText"].InnerText, Image = imageURL }; carfax.ReportList.Add(tmp); } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["FrameDamage"]["FrameDamageText"].InnerText)) { string imageURL = root["vin-info"]["WindowSticker"]["FrameDamage"]["CheckmarkImage"].InnerText.Replace("_bw", ""); var tmp = new CarFaxWindowSticker() { Text = root["vin-info"]["WindowSticker"]["FrameDamage"]["FrameDamageText"].InnerText, Image = imageURL }; carfax.ReportList.Add(tmp); } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["ManufacturerRecall"]["ManufacturerRecallText"].InnerText)) { string imageURL = root["vin-info"]["WindowSticker"]["ManufacturerRecall"]["CheckmarkImage"].InnerText.Replace("_bw", ""); var tmp = new CarFaxWindowSticker() { Text = root["vin-info"]["WindowSticker"]["ManufacturerRecall"]["ManufacturerRecallText"].InnerText, Image = imageURL }; carfax.ReportList.Add(tmp); } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["OdometerRollback"]["OdometerRollbackText"].InnerText)) { string imageURL = root["vin-info"]["WindowSticker"]["OdometerRollback"]["CheckmarkImage"].InnerText.Replace("_bw", ""); var tmp = new CarFaxWindowSticker() { Text = root["vin-info"]["WindowSticker"]["OdometerRollback"]["OdometerRollbackText"].InnerText, Image = imageURL }; carfax.ReportList.Add(tmp); } if (!String.IsNullOrEmpty(root["vin-info"]["WindowSticker"]["Disclaimer"].InnerText)) { carfax.Disclaimer = root["vin-info"]["WindowSticker"]["Disclaimer"].InnerText; } carfax.Success = true; if (carfax.ExistDatabase == 0) { SQLHelper.AddCarFaxReport(carfax); } else { SQLHelper.UpdateCarFaxReport(carfax); } } catch (Exception) { carfax.Success = false; } } else { carfax.Success = false; } carfax.CarFaxXMLResponse = root.InnerXml; } else { carfax.Success = false; } } return(carfax); }