Beispiel #1
0
        /// <summary>
        /// Method to export contacts to remote uri. Generates the
        /// xml format for the contacts and provides that to the
        /// method used to generate the POST request.
        /// </summary>
        /// <param name="uri">string</param>
        /// <param name="cred">VATRPCredential</param>
        /// <returns>void</returns>
        private async Task ExecuteRemoteExport(string uri, VATRPCredential cred)
        {
            var cardWriter = new vCardWriter();

            this.MyContacts        = new ContactList();
            this.MyContacts.VCards = new List <vCard>();

            foreach (var contactVM in this.Contacts)
            {
                var card = new vCard()
                {
                    GivenName     = contactVM.Contact.Fullname,
                    FormattedName = contactVM.Contact.Fullname,
                    Title         = contactVM.Contact.RegistrationName
                };

                card.Telephone.Uri = contactVM.Contact.RegistrationName;

                this.MyContacts.VCards.Add(card);
            }

            // Add the namespaces
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
            this.MyContacts.Namespaces = ns;

            // Serialize contacts into xml string
            if (this.MyContacts.VCards != null)
            {
                XmlAttributes atts = new XmlAttributes();
                atts.Xmlns = true;

                XmlAttributeOverrides xover = new XmlAttributeOverrides();
                xover.Add(typeof(List <vCard>), "Namespaces", atts);

                XmlSerializer xsSubmit = new XmlSerializer(typeof(List <vCard>), xover);

                var xml = "";

                //XmlWriterSettings settings = new XmlWriterSettings();
                //settings.OmitXmlDeclaration = true; // is this necessary?

                using (var sww = new StringWriter())
                {
                    using (XmlWriter writer = XmlWriter.Create(sww))
                    {
                        xsSubmit.Serialize(writer, this.MyContacts.VCards);
                        xml = sww.ToString();                        // the final xml output
                        xml = xml.Replace("ArrayOfVcard", "vcards"); // replace incorrect tag with correct one
                        xml = xml.Replace("utf-16", "utf-8");        // TODO - fix this with formatting
                    }
                }

                try
                {
                    await JsonWebRequest.MakeXmlWebPostAuthenticatedAsync(uri, cred, xml);
                }
                catch (JsonException ex)
                {
                    Debug.WriteLine($"ERROR -- Failed to POST contacts to {uri}.");
                }
            }
        }