public IActionResult Get() { string activitiesURL; IParserIATI parserIATI; XMLResultVersion2 returnResult2 = null; XMLResultVersion1 returnResult1 = null; try { string country = "BD"; string org = "CA-3"; activitiesURL = "http://datastore.iatistandard.org/api/1/access/activity.xml?recipient-country=" + country + "&reporting-org=" + org + "&stream=True"; //activitiesURL = "http://datastore.iatistandard.org/api/1/access/activity.xml?recipient-country=" + country + "&stream=True"; //Parser v2.01 parserIATI = new ParserIATIV2(); returnResult2 = (XMLResultVersion2)parserIATI.ParseIATIXML(activitiesURL); var iatiactivityArray = returnResult2?.iatiactivities?.iatiactivity; if (iatiactivityArray != null && iatiactivityArray.n()[0].AnyAttr.n()[0].Value.StartsWith("1.0")) { //Parser v1.05 parserIATI = new ParserIATIV1(); returnResult1 = (XMLResultVersion1)parserIATI.ParseIATIXML(activitiesURL); //Conversion ConvertIATIVersion2 convertIATIv2 = new ConvertIATIVersion2(); returnResult2 = convertIATIv2.ConvertIATI105to201XML(returnResult1, returnResult2); } } catch (Exception ex) { returnResult2.n().Value = ex.Message; } string jsonStr = JsonConvert.SerializeObject(returnResult2); dynamic json = JsonConvert.DeserializeObject(jsonStr); return(Ok(json)); }
/// <summary> /// Convert IATI /// </summary> /// <returns></returns> //public IXMLResult ConvertIATIXML(IXMLResult objSource, IXMLResult objDestinaiton) //{ // if(objDestinaiton == null) // objDestinaiton = new XMLResultVersion2(); // //parse and assign // return objDestinaiton; //} public XMLResultVersion2 ConvertIATI105to201XML(XMLResultVersion1 objSource, XMLResultVersion2 objDestinaiton, List <Log> logs = null) { if (objDestinaiton == null) { objDestinaiton = new XMLResultVersion2(); } //iatiactivities if (objSource != null && objSource.iatiactivities != null && objSource.iatiactivities.Items != null) { objSource.iatiactivities.version = (decimal)2.02; //activity foreach (var item in objSource.iatiactivities.Items) { if (item.GetType() == typeof(v1.iatiactivity)) { string srcIatiidentifier = ""; try { var activity = (v1.iatiactivity)item; if (activity.Items != null) { foreach (var activityItem in activity.Items) { //iati-identifier if (activityItem.GetType() == typeof(v1.iatiidentifier)) { var iatiidentifier = (v1.iatiidentifier)activityItem; srcIatiidentifier = iatiidentifier.Text.n(0); } } var desActivity = objDestinaiton.iatiactivities.iatiactivity.FirstOrDefault(q => q.IatiIdentifier == srcIatiidentifier); //desActivity.AnyAttr[0].Value = "2.02"; desActivity.location = new List <v2.location>(); desActivity.result = new List <v2.result>(); int otherIdentifierCounter = 0; foreach (var activityItem in activity.Items) { #region reporting-org if (activityItem.GetType() == typeof(v1.reportingorg)) { var reportingorg = (v1.reportingorg)activityItem; List <narrative> arrynarrative = getNarrativeList(reportingorg); desActivity.reportingorg.narrative = arrynarrative; } #endregion #region title else if (activityItem.GetType() == typeof(v1.textType)) { var title = (v1.textType)activityItem; List <narrative> arrynarrative = getNarrativeList(title); desActivity.title.narrative = arrynarrative; } #endregion #region description else if (activityItem.GetType() == typeof(v1.description)) { var description = (v1.description)activityItem; List <narrative> arrynarrative = getNarrativeList(description); desActivity.description = new List <iatiactivityDescription> { new iatiactivityDescription { narrative = arrynarrative } }; } #endregion #region participating-org else if (activityItem.GetType() == typeof(v1.participatingorg)) { var participatingorg = (v1.participatingorg)activityItem; List <narrative> arrynarrative = getNarrativeList(participatingorg); var targetParticipatingOrg = desActivity.participatingorg.FirstOrDefault(x => x.role == participatingorg.role && x.@ref == participatingorg.@ref && x.type == participatingorg.type); targetParticipatingOrg.role = getOrgRoleCode(participatingorg.role); targetParticipatingOrg.narrative = arrynarrative; } #endregion //recipient-country //Same //activity-status //Same #region activity-date else if (activityItem.GetType() == typeof(v1.activitydate)) { var activitydate = (v1.activitydate)activityItem; var targetActivitydate = desActivity.activitydate.FirstOrDefault(x => x.type == activitydate.type); targetActivitydate.type = getActivityDateCode(activitydate.type); } #endregion #region contact-info else if (activityItem.GetType() == typeof(v1.contactinfo)) { var contactinfo = (v1.contactinfo)activityItem; if (desActivity.contactinfo == null) { desActivity.contactinfo = new List <v2.contactinfo>(); } if (desActivity.contactinfo[0] == null) { desActivity.contactinfo.Add(new contactinfo()); } var desContactInfo = desActivity.contactinfo; foreach (var it in contactinfo.Items) { //organisation if (it.GetType() == typeof(v1.textType)) //[textType has multiple] { var org = (v1.textType)it; List <narrative> arrynarrative2 = getNarrativeList(org); desActivity.contactinfo[0].organisation = new textRequiredType(); desActivity.contactinfo[0].organisation.narrative = arrynarrative2; } //mailingaddress if (it.GetType() == typeof(v1.contactinfoMailingaddress)) { var addr = (v1.contactinfoMailingaddress)it; List <narrative> arrynarrative2 = getNarrativeList2(addr); desActivity.contactinfo[0].mailingaddress = new List <textRequiredType> { new textRequiredType { narrative = arrynarrative2 } }; } } } #endregion #region location else if (activityItem.GetType() == typeof(v1.location)) { var location = (v1.location)activityItem; var locationV2 = new location(); foreach (var it in location.Items) { if (it.GetType() == typeof(v1.locationCoordinates)) { var coordinate = (v1.locationCoordinates)it; locationV2.point = new locationPoint { pos = coordinate.latitude + " " + coordinate.longitude }; } else if (it.GetType() == typeof(v1.locationPoint)) { var point = (v1.locationPoint)it; locationV2.point = new locationPoint { pos = point.Items.n(0).ToString() }; } else if (it.GetType() == typeof(v1.locationAdministrative)) { var adm = (v1.locationAdministrative)it; var adm2 = new locationAdministrative { vocabulary = adm.vocabulary, level = adm.level, code = adm.code }; if (locationV2.administrative == null) { locationV2.administrative = new List <locationAdministrative> { adm2 } } ; else { locationV2.administrative.Add(adm2); } } } desActivity.location.Add(locationV2); } #endregion //sector //same //policy-marker //same //collaboration-type //same //default-finance-type //same #region budget else if (activityItem.GetType() == typeof(v1.budget)) { var budget = (v1.budget)activityItem; foreach (var b in desActivity.budget) { b.type = budget.type == "Original" ? "1" : "2"; } } #endregion //planned-disbursement //not in 1.05 #region transaction else if (activityItem.GetType() == typeof(v1.transaction)) { var transaction = (v1.transaction)activityItem; var targettransaction = desActivity.transaction.FirstOrDefault(x => x.transactiontype.code == transaction.transactiontype.code); targettransaction.transactiontype.code = gettransactionCode(transaction.transactiontype.code); //------------------ } #endregion #region document - link else if (activityItem.GetType() == typeof(v1.documentlink)) { var documentlink = (v1.documentlink)activityItem; var d = documentlink.Items.FirstOrDefault(x => x.GetType() == typeof(v1.textType)); if (d != null) { List <narrative> arrynarrative = getNarrativeList((v1.textType)d); var targetdocumentlink = desActivity.documentlink.FirstOrDefault(x => x.url == documentlink.url); targetdocumentlink.title = new textRequiredType(); targetdocumentlink.title.narrative = arrynarrative; } } #endregion //conditions //Not in 1.05 //result #region result else if (activityItem.GetType() == typeof(v1.result)) { var result = (v1.result)activityItem; var resultV2 = new result(); resultV2.indicator = new List <resultIndicator>(); foreach (var resultItem in result.Items) { if (resultItem.GetType() == typeof(v1.textType)) { var title = (v1.textType)resultItem; resultV2.title = new textRequiredType { narrative = getNarrativeList(resultItem) }; } else if (resultItem.GetType() == typeof(v1.description)) { var description = (v1.description)resultItem; resultV2.description = new description { narrative = getNarrativeList(resultItem) }; } else if (resultItem.GetType() == typeof(v1.resultIndicator)) { var indicator = (v1.resultIndicator)resultItem; var indicatorV2 = new resultIndicator(); indicatorV2.period = new List <resultIndicatorPeriod>(); foreach (var indicatorItem in indicator.Items) { if (indicatorItem.GetType() == typeof(v1.textType)) { var title = (v1.textType)indicatorItem; indicatorV2.title = new textRequiredType { narrative = getNarrativeList(indicatorItem) }; } else if (indicatorItem.GetType() == typeof(v1.description)) { var desc = (v1.description)indicatorItem; indicatorV2.description = new description { narrative = getNarrativeList(indicatorItem) }; } else if (indicatorItem.GetType() == typeof(v1.resultIndicatorBaseline)) { var baseline = (v1.resultIndicatorBaseline)indicatorItem; indicatorV2.baseline = new resultIndicatorBaseline { year = baseline.year, value = baseline.value }; } else if (indicatorItem.GetType() == typeof(v1.resultIndicatorPeriod)) { var period = (v1.resultIndicatorPeriod)indicatorItem; var periodV2 = new resultIndicatorPeriod(); v1.dateType date1 = null; v1.dateType date2 = null; foreach (var periodItem in period.Items) { if (periodItem.GetType() == typeof(v1.dateType)) { if (date1 == null) { date1 = (v1.dateType)periodItem; } else { date2 = (v1.dateType)periodItem; } } else if (periodItem.GetType() == typeof(v1.resultIndicatorPeriodActual)) { var periodActual = (v1.resultIndicatorPeriodActual)periodItem; periodV2.actual = new resultIndicatorPeriodActual { value = periodActual.value }; } else if (periodItem.GetType() == typeof(v1.resultIndicatorPeriodTarget)) { var periodTarget = (v1.resultIndicatorPeriodTarget)periodItem; periodV2.target = new resultIndicatorPeriodTarget { value = periodTarget.value }; } } date1 = date1 ?? date2; date2 = date2 ?? date1; periodV2.periodstart = new resultIndicatorPeriodPeriodstart { isodate = date1.isodate < date2.isodate ? date1.isodate : date2.isodate }; periodV2.periodend = new resultIndicatorPeriodPeriodend { isodate = date1.isodate > date2.isodate ? date1.isodate : date2.isodate }; indicatorV2.period.Add(periodV2); } } resultV2.indicator.Add(indicatorV2); } } desActivity.result.Add(resultV2); } #endregion #region other-identifier else if (activityItem.GetType() == typeof(v1.otheridentifier)) { var otheridentifier = (v1.otheridentifier)activityItem; List <narrative> arrynarrative = Statix.GetNarrativeList(otheridentifier.ownername); var targetotheridentifier = desActivity.otheridentifier[otherIdentifierCounter]; targetotheridentifier.@ref = otheridentifier.Text.n(0); targetotheridentifier.type = "A1"; targetotheridentifier.ownerorg = new otheridentifierOwnerorg(); targetotheridentifier.ownerorg.@ref = otheridentifier.ownerref; targetotheridentifier.ownerorg.narrative = arrynarrative; targetotheridentifier.AnyAttr = null; otherIdentifierCounter++; } #endregion } } } catch (Exception ex) { throw ex; } } } } //parse and assign return(objDestinaiton); }