public static XmlElement SerializeIREnvelope(IRenvelope ire) { using (MemoryStream memStream = new MemoryStream()) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(String.Empty, "http://www.govtalk.gov.uk/taxation/charities/r68/2"); var knownTypes = new Type[] { typeof(IRenvelope), typeof(R68Claim) }; XmlSerializer serializer = new XmlSerializer(typeof(IRenvelope), knownTypes); XmlTextWriter tw = new XmlTextWriter(memStream, UTF8Encoding.UTF8); XmlDocument doc = new XmlDocument(); tw.Formatting = Formatting.Indented; // tw.IndentChar = '\x09'; //tab tw.IndentChar = ' '; serializer.Serialize(tw, ire, ns); memStream.Seek(0, SeekOrigin.Begin); doc.Load(memStream); XmlElement returnVal = doc.DocumentElement; return(returnVal); } }
public static XmlDocument TestDeserializeAndDecompress(XmlDocument xd) { GovTalkMessage gtm = XmlSerializationHelpers.DeserializeMessage(xd); XmlElement xelement = gtm.Body.Any[0]; // XmlDocument bodyDoc = xelement.OwnerDocument; XmlDocument bodyDoc = new XmlDocument(); bodyDoc.LoadXml(xelement.OuterXml); IRenvelope ire = XmlSerializationHelpers.DeserializeIRenvelope(bodyDoc); R68CompressedPart compressedPart = (R68CompressedPart)ire.R68.Items[0]; string decompressedClaim = CommonUtilityHelper.DecompressData(compressedPart.Value); string decompressedR68Claim = decompressedClaim.Replace("Claim", "R68Claim"); XmlDocument r68claim = new XmlDocument(); r68claim.LoadXml(decompressedR68Claim); r68claim.DocumentElement.SetAttribute("xmlns", "http://www.govtalk.gov.uk/taxation/charities/r68/2"); R68Claim uncompressedR68 = XmlSerializationHelpers.Deserialize <R68Claim>(r68claim.OuterXml, "R68Claim"); // XmlSerializationHelpers.DeserializeR68Claim(r68claim); ire.R68.Items[0] = uncompressedR68; gtm.Body.Any[0] = XmlSerializationHelpers.SerializeIREnvelope(ire); XmlDocument SerializedDecompressedGovTalkMessage = XmlSerializationHelpers.SerializeGovTalkMessage(gtm); return(SerializedDecompressedGovTalkMessage); }
// read message into datatable of donations ? // what do we want to do when we read in a Submit Request - recreate the input datatables (donations & otherincome) // list of the reference values public void ReadMessage(XDocument inXD) { _message = XmlSerializationHelpers.DeserializeMessage(inXD.ToXmlDocument()); _messageRead = true; _qualifier = _message.Header.MessageDetails.Qualifier.ToString(); _function = _message.Header.MessageDetails.Function.ToString(); XmlElement xmlElement = _message.Body.Any[0]; XmlDocument bodyDoc = new XmlDocument(); bodyDoc.LoadXml(xmlElement.OuterXml); _body = XmlSerializationHelpers.DeserializeIRenvelope(bodyDoc); if (_body.R68.Items[0].GetType() == typeof(R68CompressedPart)) { _hasCompressedPart = true; } else { _hasCompressedPart = false; } _loggingService.LogInfo(this, "Message read. Message is SubmitRequest."); }
public XmlElement SerializeIREnvelope(IRenvelope irEnvelope) { // Always the same regardless of nature of IRenvelope, right? // Is this the right place for it - does the builder of IRenvelopes also serialize them? using (MemoryStream memStream = new MemoryStream()) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(String.Empty, "http://www.govtalk.gov.uk/taxation/charities/r68/1"); var knownTypes = new Type[] { typeof(IRenvelope), typeof(R68Claim) }; XmlSerializer serializer = new XmlSerializer(typeof(IRenvelope), knownTypes); XmlTextWriter tw = new XmlTextWriter(memStream, UTF8Encoding.UTF8); XmlDocument doc = new XmlDocument(); tw.Formatting = Formatting.Indented; tw.IndentChar = ' '; serializer.Serialize(tw, irEnvelope, ns); memStream.Seek(0, SeekOrigin.Begin); doc.Load(memStream); XmlElement returnVal = doc.DocumentElement; return(returnVal); } }
private static XNode IREnvelopeToXDocument(IRenvelope envelope) { if (envelope != null) { XNamespace ns = "http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2"; XDocument doc = new XDocument( new XDeclaration("1.0", "UTF-16", null), new XElement(ns + "IRenvelope", new XElement(ns + "IRheader", new XElement(ns + "Keys", new XElement(ns + "Key", new XAttribute("Type", "VATRegNo"), envelope.IRheader.Keys.Select(x => x.Value).FirstOrDefault().ToString())), new XElement(ns + "PeriodID", envelope.IRheader.PeriodID.ToString()), // new XElement(ns + "PeriodStart", envelope.IRheader.PeriodStart.ToString("yyyy-MM-dd")), //new XElement(ns + "PeriodEnd", envelope.IRheader.PeriodEnd.ToString("yyyy-MM-dd")), new XElement(ns + "Principal", new XElement(ns + "Contact", new XElement(ns + "Name", new XElement(ns + "Ttl", envelope.IRheader.Principal.Contact.Name.Ttl.ToString()), new XElement(ns + "Fore", envelope.IRheader.Principal.Contact.Name.Fore.GetValue(0).ToString()), new XElement(ns + "Fore", !String.IsNullOrEmpty(envelope.IRheader.Principal.Contact.Name.Fore.Last() ?? "")), new XElement(ns + "Sur", envelope.IRheader.Principal.Contact.Name.Sur.ToString()) ), new XElement(ns + "Email", //new XAttribute("Type", // envelope.IRheader.Principal.Contact.Email.Select(x => x.Type).FirstOrDefault().ToString()), //new XAttribute("Preferred", // envelope.IRheader.Principal.Contact.Email.Select(x => x.Preferred).FirstOrDefault() // .ToString()), envelope.IRheader.Principal.Contact.Email.Select(x => x.Value).FirstOrDefault().ToString())) ), new XElement(ns + "IRmark", new XAttribute("Type", "generic"), ""), new XElement(ns + "Sender", envelope.IRheader.Sender.ToString()) ), new XElement(ns + "VATDeclarationRequest", new XElement(ns + "VATDueOnOutputs", envelope.VATDeclarationRequest.VATDueOnOutputs.ToString()), new XElement(ns + "VATDueOnECAcquisitions", envelope.VATDeclarationRequest.VATDueOnECAcquisitions.ToString()), new XElement(ns + "TotalVAT", envelope.VATDeclarationRequest.TotalVAT.ToString()), new XElement(ns + "VATReclaimedOnInputs", envelope.VATDeclarationRequest.VATReclaimedOnInputs.ToString()), new XElement(ns + "NetVAT", envelope.VATDeclarationRequest.NetVAT.ToString()), new XElement(ns + "NetSalesAndOutputs", envelope.VATDeclarationRequest.NetSalesAndOutputs.ToString()), new XElement(ns + "NetPurchasesAndInputs", envelope.VATDeclarationRequest.NetPurchasesAndInputs.ToString()), new XElement(ns + "NetECSupplies", envelope.VATDeclarationRequest.NetECSupplies.ToString()), new XElement(ns + "NetECAcquisitions", envelope.VATDeclarationRequest.NetECAcquisitions.ToString()) ) )); //foreach (var element in doc.Descendants()) //just add namespace using for each instead of hard coding //element.Name = aw + element.Name.LocalName; return(doc); } return(null); }
public static string SerializeIrEnvelope(IRenvelope iRenvelope) { using (StringWriter sw = new StringWriter()) { var serializer = new XmlSerializer(typeof(IRenvelope)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ConfigurationManager.AppSettings["IRurl"].ToString()); serializer.Serialize(sw, iRenvelope, ns); var xmlString = sw.ToString(); return(xmlString); } }
public static IRenvelope DeserializeIRenvelope(XmlDocument xmlElement) { XmlSerializer serializer = new XmlSerializer(typeof(IRenvelope)); MemoryStream xmlStream = new MemoryStream(); xmlElement.Save(xmlStream); xmlStream.Seek(0, SeekOrigin.Begin); var LocalIRenvelope = serializer.Deserialize(xmlStream); IRenvelope ire = (IRenvelope)LocalIRenvelope; return(ire); }
public XmlDocument CompressClaim() { _loggingService.LogInfo(this, "Compressing Claim."); XmlElement bodyElement = _govTalkMessageBuilder.GovTalkMessage.Body.Any[0]; XmlDocument bodyDocument = new XmlDocument(); bodyDocument.LoadXml(bodyElement.OuterXml); //deserialize body IRenvelope irEnvelope = XmlSerializationHelpers.DeserializeIRenvelope(bodyDocument); R68 uncompressedR68 = irEnvelope.R68; XmlDocument r68xmlDoc = XmlSerializationHelpers.SerializeItem(uncompressedR68); System.Xml.XmlDocument claimXmlDoc = GovTalkMessageHelper.GetClaim(r68xmlDoc); irEnvelope.R68.Items = null; R68CompressedPart compressedPart = new R68CompressedPart(); compressedPart.Type = R68CompressedPartType.gzip; compressedPart.Value = CommonUtilityHelper.CompressData(claimXmlDoc.OuterXml, _loggingService); R68CompressedPart[] compressedParts = new R68CompressedPart[1]; compressedParts[0] = compressedPart; irEnvelope.R68.Items = compressedParts; bodyElement = XmlSerializationHelpers.SerializeIREnvelope(irEnvelope); _govTalkMessageBuilder.GovTalkMessage.Body.Any[0] = null; _govTalkMessageBuilder.GovTalkMessage.Body.Any[0] = bodyElement; XmlDocument compressedVersion = SerializeGovTalkMessage(); return(compressedVersion); }
public IRenvelope CreateIREnvelopeBody(Sender sender, Business business) { IRenvelope irenvelope = new IRenvelope(); IRheader irheader = new IRheader(); IRheaderKey irheaderkey = new IRheaderKey(); irheaderkey.Type = "VATRegNo"; irheaderkey.Value = business.VATRegNo; irheader.PeriodID = _periodData.PeriodrefId; irheader.Sender = IRheaderSender.Individual; irheader.PeriodStart = _periodData.StartPeriod; irheader.PeriodEnd = _periodData.EndPeriod; IRheaderKey[] irHeaderKeys = new IRheaderKey[1]; irHeaderKeys[0] = irheaderkey; irheader.Keys = irHeaderKeys; VATDeclarationRequest_ContactDetailsStructure contact = new VATDeclarationRequest_ContactDetailsStructure(); VATDeclarationRequest_ContactDetailsStructureName name = new VATDeclarationRequest_ContactDetailsStructureName(); name.Fore = new[] { sender.ForName1, sender.ForName2 }; name.Sur = sender.SurName; name.Ttl = sender.Title; VATDeclarationRequest_ContactDetailsStructureEmail email = new VATDeclarationRequest_ContactDetailsStructureEmail(); email.Preferred = VATDeclarationRequest_YesNoType.yes; email.PreferredSpecified = true; email.Type = VATDeclarationRequest_WorkHomeType.work; email.TypeSpecified = true; email.Value = sender.Email; contact.Name = name; VATDeclarationRequest_ContactDetailsStructureEmail[] Emails = new VATDeclarationRequest_ContactDetailsStructureEmail[1]; Emails[0] = email; contact.Email = Emails; IRheaderPrincipal principal = new IRheaderPrincipal(); principal.Contact = contact; irheader.Principal = principal; IRheaderIRmark irmark = new IRheaderIRmark(); irmark.Type = IRheaderIRmarkType.generic; irmark.Value = ""; irheader.IRmark = irmark; VATDeclarationRequest vatreq = new VATDeclarationRequest(); vatreq.VATDueOnOutputs = _vat100Data.Box1; vatreq.VATDueOnECAcquisitions = _vat100Data.Box2; vatreq.TotalVAT = _vat100Data.Box3; vatreq.VATReclaimedOnInputs = _vat100Data.Box4; vatreq.NetVAT = _vat100Data.Box5; vatreq.NetSalesAndOutputs = _vat100Data.Box6; vatreq.NetPurchasesAndInputs = _vat100Data.Box7; vatreq.NetECSupplies = _vat100Data.Box8; vatreq.NetECAcquisitions = _vat100Data.Box9; //set values here irenvelope.IRheader = irheader; irenvelope.VATDeclarationRequest = vatreq; return(irenvelope); }
public GovTalkMessage CreateGovTalkMessage(Sender sender, Business business, PeriodData periodData, VAT100 vatData, bool testInLive) { string classType = !testInLive ? "HMRC-VAT-DEC" : "HMRC-VAT-DEC-TIL"; string gatewayTest = !testInLive ? "0" : "1"; //Gov Talk message GovTalkMessage govTalkMessage = new GovTalkMessage(); //Gov Talk header GovTalkMessageHeader header = new GovTalkMessageHeader(); //sender details GovTalkMessageHeaderSenderDetails senderDetails = new GovTalkMessageHeaderSenderDetails(); GovTalkMessageHeaderSenderDetailsIDAuthentication idauth = new GovTalkMessageHeaderSenderDetailsIDAuthentication(); idauth.SenderID = sender.HMRCUserId; GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication aauth = new GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication(); aauth.Method = GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthenticationMethod.clear; aauth.Role = "principal"; aauth.Item = sender.HMRCPassword; GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication[] authentications = new GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication[1]; authentications[0] = aauth; idauth.Authentication = authentications; senderDetails.IDAuthentication = idauth; senderDetails.EmailAddress = sender.Email; //message details GovTalkMessageHeaderMessageDetails messagedetails = new GovTalkMessageHeaderMessageDetails(); messagedetails.Class = classType; messagedetails.Qualifier = GovTalkMessageHeaderMessageDetailsQualifier.request; messagedetails.Function = GovTalkMessageHeaderMessageDetailsFunction.submit; messagedetails.FunctionSpecified = true; messagedetails.CorrelationID = ""; messagedetails.Transformation = GovTalkMessageHeaderMessageDetailsTransformation.XML; messagedetails.GatewayTest = gatewayTest; messagedetails.TransformationSpecified = true; header.MessageDetails = messagedetails; header.SenderDetails = senderDetails; //Gov Talk details GovTalkMessageGovTalkDetails govTalkDetails = new GovTalkMessageGovTalkDetails(); //keys GovTalkMessageGovTalkDetailsKey key = new GovTalkMessageGovTalkDetailsKey(); key.Type = ConfigurationManager.AppSettings["GovTalkDetailsKeyType"]; key.Value = business.VATRegNo.ToString(); GovTalkMessageGovTalkDetailsKey[] keys = new GovTalkMessageGovTalkDetailsKey[1]; keys[0] = key; //Channel routing GovTalkMessageGovTalkDetailsChannelRoutingChannel channel = new GovTalkMessageGovTalkDetailsChannelRoutingChannel(); channel.Version = ConfigurationManager.AppSettings["ChannelVersion"]; channel.Product = ConfigurationManager.AppSettings["ChannelProduct"]; channel.ItemElementName = ItemChoiceType.URI; channel.Item = ConfigurationManager.AppSettings["ChannelUri"]; GovTalkMessageGovTalkDetailsChannelRouting channelRouting = new GovTalkMessageGovTalkDetailsChannelRouting(); channelRouting.Channel = channel; GovTalkMessageGovTalkDetailsChannelRouting[] channelRoutings = new GovTalkMessageGovTalkDetailsChannelRouting[1]; channelRoutings[0] = channelRouting; govTalkDetails.Keys = keys; govTalkDetails.ChannelRouting = channelRoutings; //Gov Talk Body GovTalkMessageBody body = new GovTalkMessageBody(); //populate body content with ir68 data //IR68 ir68 = new IR68(periodData, vatData); //commented out to use method instead of object instantiation with period/vat data //IRenvelope iRenvelope = ir68.CreateIREnvelopeBody(sender, business); IRenvelope iRenvelope = CreateIRenvelopeBody(sender, business, periodData, vatData); //serialse ir object data into xml elements and add to Gov Talk body //XElement xe = XElement.Parse(SerializeIrEnvelope(iRenvelope)); XElement xe = XElement.Parse(IREnvelopeToXDocument(iRenvelope).ToString()); XmlElement xelement = ToXmlElement(xe); XmlElement[] xmlElements = new XmlElement[1]; xmlElements[0] = xelement; body.Any = xmlElements; govTalkMessage.EnvelopeVersion = ConfigurationManager.AppSettings["GovTalkMessageEnvelopeVersion"]; govTalkMessage.Header = header; govTalkMessage.GovTalkDetails = govTalkDetails; govTalkMessage.Body = body; return(govTalkMessage); }
private IRenvelope CreateIRenvelopeBody(Sender sender, Business business, PeriodData periodData, VAT100 vatData) { IRenvelope irenvelope = new IRenvelope(); IRheader irheader = new IRheader(); IRheaderKey irheaderkey = new IRheaderKey { Type = "VATRegNo", Value = business.VATRegNo }; irheader.PeriodID = periodData.PeriodrefId; irheader.Sender = IRheaderSender.Individual; // irheader.PeriodStart = periodData.StartPeriod; //irheader.PeriodEnd = periodData.EndPeriod; IRheaderKey[] irHeaderKeys = new IRheaderKey[1]; irHeaderKeys[0] = irheaderkey; irheader.Keys = irHeaderKeys; VATDeclarationRequest_ContactDetailsStructure contact = new VATDeclarationRequest_ContactDetailsStructure(); VATDeclarationRequest_ContactDetailsStructureName name = new VATDeclarationRequest_ContactDetailsStructureName { Fore = new[] { sender.ForName1, sender.ForName2 }, Sur = sender.SurName, Ttl = sender.Title }; VATDeclarationRequest_ContactDetailsStructureEmail email = new VATDeclarationRequest_ContactDetailsStructureEmail { Preferred = VATDeclarationRequest_YesNoType.yes, PreferredSpecified = true, Type = VATDeclarationRequest_WorkHomeType.work, TypeSpecified = true, Value = sender.Email }; contact.Name = name; VATDeclarationRequest_ContactDetailsStructureEmail[] Emails = new VATDeclarationRequest_ContactDetailsStructureEmail[1]; Emails[0] = email; contact.Email = Emails; IRheaderPrincipal principal = new IRheaderPrincipal { Contact = contact }; irheader.Principal = principal; IRheaderIRmark irmark = new IRheaderIRmark(); irmark.Type = IRheaderIRmarkType.generic; irmark.Value = ""; irheader.IRmark = irmark; VATDeclarationRequest vatreq = new VATDeclarationRequest { VATDueOnOutputs = vatData.Box1, VATDueOnECAcquisitions = vatData.Box2, TotalVAT = vatData.Box3, VATReclaimedOnInputs = vatData.Box4, NetVAT = vatData.Box5, NetSalesAndOutputs = vatData.Box6, NetPurchasesAndInputs = vatData.Box7, NetECSupplies = vatData.Box8, NetECAcquisitions = vatData.Box9 }; //set values here irenvelope.IRheader = irheader; irenvelope.VATDeclarationRequest = vatreq; return(irenvelope); }