/// <summary> /// Method to export contacts to local machine. Creates /// the file the contacts will be saved to and calls methods /// that generate the xml formatting for the exported contacts. /// </summary> /// <returns>void</returns> private void ExecuteLocalExport() { var saveDlg = new SaveFileDialog() { CheckPathExists = true, OverwritePrompt = true, FileName = "ace_contacts", Filter = "VCF files (*.vcf) | *.vcf| xCard files (*.xml) | *.xml", FilterIndex = 0, }; if (saveDlg.ShowDialog() != true) { return; } if (ServiceManager.Instance.LinphoneService.VCardSupported) { // Added 2/21/207 fjr, save as XML if (saveDlg.FileName.EndsWith(".xml")) { var cardWriter = new vCardWriter(); var 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 }; vCards.Add(card); } cardWriter.WriteCardsAsXML(saveDlg.FileName, vCards); } else { ServiceManager.Instance.ContactService.ExportVCards(saveDlg.FileName); } } else { var cardWriter = new vCardWriter(); var 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 }; vCards.Add(card); } cardWriter.WriteCards(saveDlg.FileName, vCards); } }
/// <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}."); } } }