w określonej strukturze. Nie wiem czy to można nazwać programem, ale było bardzo przydatne przy rozwiązaniu kilku problemów.
Ja generowałem wcześniej xml z jakiegoś programu do tworzenia danych testowych. Program spełnił swoje zadanie i oszczędził mi dużo czasu.
Program tworzy wiele pojedynczych plików z kontaktami - pliki *.contact. Oczywiście xml nie musi zawierać wszystkich pól. Podane poniżej są obsługiwane.
<record>Dostępny projekt do pobrania - przeglądaj dysk google.
<note>jakiś tekst</note>
<mail>maill</mail>
<mail2>maill</mail2>
<nick>nick</nick>
<title>teskt</title>
<last>tekst testowy</last>
<middle></middle>
<first></first>
<work country></work country>
<work postal code></work postal code>
<work state></work state>
<work city></work city>
<work street></work street>
<country></country>
<postalcode></postalcode>
<state></state>
<city></city>
<street></street>
<work pager></work pager>
<work fax></work fax>
<work phone></work phone>
<cell></cell>
<fax></fax>
<phone></phone>
<website></website>
<office></office>
<department></department>
<job title></job title>
<company></company>
</record>
<record>
...
</record>
</records>
Kod źródłowy:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace kontaktywindows
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private String usun_specjalne(String ss)
{
//return Uri.EscapeUriString(ss);
ss = ss.Replace("&", "&");
ss = ss.Replace("<", "<");
return ss;
}
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
if (this.openFileDialog1.FileName != null)
{
if (System.IO.File.Exists(this.openFileDialog1.FileName))
{
this.openFileDialog1.OpenFile();
System.IO.StreamReader sr = new System.IO.StreamReader(this.openFileDialog1.FileName,Encoding.UTF8);
String s;
String formatted_name = "";
String linia = "";
List<String> list;
list = new List<String>();
int id = 1;
while ((s = sr.ReadLine()) != "</records>")
{
while (s.Contains("\t"))
s = s.Replace("\t", "");
if (s.Contains("<record>"))
{
linia = "";
list.Add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
list.Add("<c:contact c:Version=\"1\" xmlns:c=\"http://schemas.microsoft.com/Contact\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:MSP2P=\"http://schemas.microsoft.com/Contact/Extended/MSP2P\" xmlns:MSWABMAPI=\"http://schemas.microsoft.com/Contact/Extended/MSWABMAPI\">");
}
if (s.Contains("<note>"))
{
s = s.Remove(0, "<note>".Length);
s = s.Remove((s.Length - "</note>".Length), "</note>".Length);
linia = "<c:Notes>" + this.usun_specjalne(s);
linia = linia + "</c:Notes><c:Gender>Male</c:Gender><c:CreationDate>2011-11-02T11:33:06Z</c:CreationDate><c:Extended><MSWABMAPI:PropTag0x3A58101F c:ContentType=\"binary/x-ms-wab-mapi\" c:type=\"binary\">AgAAAAwAAABjAGgAaQBsAGQAAAAOAAAAYwBoAGkAbABkAGQAAAA=</MSWABMAPI:PropTag0x3A58101F></c:Extended>";
list.Add(linia);
}
if (s.Contains("<mail>"))
{
linia = "";
linia = "<c:ContactIDCollection><c:ContactID c:ElementID=\"d2bcb649-0b11-49e0-aa3e-32855dbf4ede\"><c:Value>49b1c7e3-ab89-4989-b4f9-3e92c065d330</c:Value></c:ContactID></c:ContactIDCollection><c:EmailAddressCollection><c:EmailAddress c:ElementID=\"6638fcf5-abd4-4f4a-a411-2d35a0898fd5\"><c:Type>SMTP</c:Type><c:Address>";
s = s.Remove(0, "<mail>".Length);
s = s.Remove((s.Length - "</mail>".Length), "</mail>".Length);
linia = linia + this.usun_specjalne(s);
linia = linia + "</c:Address><c:LabelCollection><c:Label>Preferred</c:Label></c:LabelCollection></c:EmailAddress><c:EmailAddress c:ElementID=\"914546a6-d45c-49f9-aae6-0796d3d3915a\"><c:Type>SMTP</c:Type><c:Address>";
}
if (s.Contains("<mail2>"))
{
s = s.Remove(0, "<mail2>".Length);
s = s.Remove((s.Length - "</mail2>".Length), "</mail2>".Length);
linia = linia + this.usun_specjalne(s);
linia = linia + "</c:Address></c:EmailAddress><c:EmailAddress c:ElementID=\"bb10129a-a318-487a-b5b9-4ed48b6e36ed\" xsi:nil=\"true\"/></c:EmailAddressCollection><c:NameCollection><c:Name c:ElementID=\"54897d81-37aa-438b-a03c-5e4ed2fe69bc\">";
}
if (s.Contains("<nick>"))
{
formatted_name = "";
s = s.Remove(0, "<nick>".Length);
s = s.Remove((s.Length - "</nick>".Length), "</nick>".Length);
linia = linia + "<c:NickName>" + this.usun_specjalne(s) + "</c:NickName>";
formatted_name = this.usun_specjalne(s);
}
if (s.Contains("<title>"))
{
s = s.Remove(0, "<title>".Length);
s = s.Remove((s.Length - "</title>".Length), "</title>".Length);
linia = linia + "<c:Title>" + this.usun_specjalne(s) + "</c:Title>";
formatted_name = formatted_name + this.usun_specjalne(s);
linia = linia + "<c:FormattedName>" + formatted_name + "</c:FormattedName>";
}
if (s.Contains("<last>"))
{
s = s.Remove(0, "<last>".Length);
s = s.Remove((s.Length - "</last>".Length), "</last>".Length);
linia = linia + "<c:FamilyName>" + this.usun_specjalne(s) + "</c:FamilyName>";
}
if (s.Contains("<middle>"))
{
s = s.Remove(0, "<middle>".Length);
s = s.Remove((s.Length - "</middle>".Length), "</middle>".Length);
linia = linia + "<c:MiddleName>" + this.usun_specjalne(s) + "</c:MiddleName>";
}
if (s.Contains("<first>"))
{
s = s.Remove(0, "<first>".Length);
s = s.Remove((s.Length - "</first>".Length), "</first>".Length);
linia = linia + "<c:GivenName>" + this.usun_specjalne(s) + "</c:GivenName>";
linia = linia + "</c:Name></c:NameCollection><c:PhysicalAddressCollection><c:PhysicalAddress c:ElementID=\"42b40d43-8ca6-4288-a660-1391b3368806\">";
}
if (s.Contains("<work country>"))
{
s = s.Remove(0, "<work country>".Length);
s = s.Remove((s.Length - "</work country>".Length), "</work country>".Length);
linia = linia + "<c:Country>" + this.usun_specjalne(s) + "</c:Country>";
}
if (s.Contains("<work postal code>"))
{
s = s.Remove(0, "<work postal code>".Length);
s = s.Remove((s.Length - "</work postal code>".Length), "</work postal code>".Length);
linia = linia + "<c:PostalCode>" + this.usun_specjalne(s) + "</c:PostalCode>";
}
if (s.Contains("<work state>"))
{
s = s.Remove(0, "<work state>".Length);
s = s.Remove((s.Length - "/<work state>".Length), "</work state>".Length);
linia = linia + "<c:Region>" + this.usun_specjalne(s) + "</c:Region>";
}
if (s.Contains("<work city"))
{
s = s.Remove(0, "<work city>".Length);
s = s.Remove((s.Length - "</work city>".Length), "</work city>".Length);
linia = linia + "<c:Locality>" + this.usun_specjalne(s) + "</c:Locality>";
}
if (s.Contains("<work street>"))
{
s = s.Remove(0, "<work street>".Length);
s = s.Remove((s.Length - "</work street>".Length), "</work street>".Length);
linia = linia + "<c:Street>" + this.usun_specjalne(s) + "</c:Street>";
linia = linia + "<c:LabelCollection><c:Label>Business</c:Label></c:LabelCollection></c:PhysicalAddress><c:PhysicalAddress c:ElementID=\"aff0aab1-d53c-41cb-8046-1d5ff5653b99\">";
}
if (s.Contains("<country>"))
{
s = s.Remove(0, "<country>".Length);
s = s.Remove((s.Length - "</country>".Length), "</country>".Length);
linia = linia + "<c:Country>" + this.usun_specjalne(s) + "</c:Country>";
}
if (s.Contains("<postalcode>"))
{
s = s.Remove(0, "<postalcode>".Length);
s = s.Remove((s.Length - "</postalcode>".Length), "</postalcode>".Length);
linia = linia + "<c:PostalCode>" + this.usun_specjalne(s) + "</c:PostalCode>";
}
if (s.Contains("<state>"))
{
s = s.Remove(0, "<state>".Length);
s = s.Remove((s.Length - "</state>".Length), "</state>".Length);
linia = linia + "<c:Region>" + this.usun_specjalne(s) + "</c:Region>";
}
if (s.Contains("<city>"))
{
s = s.Remove(0, "<city>".Length);
s = s.Remove((s.Length - "</city>".Length), "</city>".Length);
linia = linia + "<c:Locality>" + this.usun_specjalne(s) + "</c:Locality>";
}
if (s.Contains("<street>"))
{
s = s.Remove(0, "<street>".Length);
s = s.Remove((s.Length - "</street>".Length), "</street>".Length);
linia = linia + "<c:Street>" + this.usun_specjalne(s) + "</c:Street>";
linia = linia + "<c:LabelCollection><c:Label>Personal</c:Label></c:LabelCollection></c:PhysicalAddress></c:PhysicalAddressCollection><c:PhoneNumberCollection><c:PhoneNumber c:ElementID=\"77e15648-c3fa-4bf8-b93a-dcb370b68ffe\">";
}
if (s.Contains("<work pager>"))
{
s = s.Remove(0, "<work pager>".Length);
s = s.Remove((s.Length - "</work pager>".Length), "</work pager>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Pager</c:Label></c:LabelCollection></c:PhoneNumber><c:PhoneNumber c:ElementID=\"8b4e501e-382d-480e-a4db-7a9028fad5b2\">";
}
if (s.Contains("<work fax>"))
{
s = s.Remove(0, "<work fax>".Length);
s = s.Remove((s.Length - "</work fax>".Length), "</work fax>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Fax</c:Label><c:Label>Business</c:Label></c:LabelCollection></c:PhoneNumber><c:PhoneNumber c:ElementID=\"01c40bdc-8456-4f43-9c79-bbd6c3ad8233\">";
}
if (s.Contains("<work phone>"))
{
s = s.Remove(0, "<work phone>".Length);
s = s.Remove((s.Length - "</work phone>".Length), "</work phone>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Voice</c:Label><c:Label>Business</c:Label></c:LabelCollection></c:PhoneNumber><c:PhoneNumber c:ElementID=\"44130e67-5a5b-4349-bb20-10bac426279b\">";
}
if (s.Contains("<cell>"))
{
s = s.Remove(0, "<cell>".Length);
s = s.Remove((s.Length - "</cell>".Length), "</cell>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Cellular</c:Label></c:LabelCollection></c:PhoneNumber><c:PhoneNumber c:ElementID=\"6b9829ad-3b4c-48d6-a635-21cbe70b445b\">";
}
if (s.Contains("<fax>"))
{
s = s.Remove(0, "<fax>".Length);
s = s.Remove((s.Length - "</fax>".Length), "</fax>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Fax</c:Label><c:Label>Personal</c:Label></c:LabelCollection></c:PhoneNumber><c:PhoneNumber c:ElementID=\"59970aa7-b922-41eb-bd5c-51926d81517e\">";
}
if (s.Contains("<phone>"))
{
s = s.Remove(0, "<phone>".Length);
s = s.Remove((s.Length - "</phone>".Length), "</phone>".Length);
linia = linia + "<c:Number>" + this.usun_specjalne(s) + "</c:Number>";
linia = linia + "<c:LabelCollection><c:Label>Voice</c:Label><c:Label>Personal</c:Label></c:LabelCollection></c:PhoneNumber></c:PhoneNumberCollection><c:UrlCollection><c:Url c:ElementID=\"ae09bacb-0f5b-4d2c-864f-e27ee18a3569\">";
}
if (s.Contains("<website>"))
{
s = s.Remove(0, "<website>".Length);
s = s.Remove((s.Length - "</website>".Length), "</website>".Length);
linia = linia + "<c:Value>" + this.usun_specjalne(s) + "</c:Value>";
linia = linia + "<c:LabelCollection><c:Label>Personal</c:Label></c:LabelCollection></c:Url></c:UrlCollection><c:PositionCollection><c:Position c:ElementID=\"c6aae636-c2a6-4b12-a14e-fca2c08d079e\">";
}
if (s.Contains("<office>"))
{
s = s.Remove(0, "<office>".Length);
s = s.Remove((s.Length - "</office>".Length), "</office>".Length);
linia = linia + "<c:Office>" + this.usun_specjalne(s) + "</c:Office>";
}
if (s.Contains("<department>"))
{
s = s.Remove(0, "<department>".Length);
s = s.Remove((s.Length - "</department>".Length), "</department>".Length);
linia = linia + "<c:Department>" + this.usun_specjalne(s) + "</c:Department>";
}
if (s.Contains("<job title>"))
{
s = s.Remove(0, "<job title>".Length);
s = s.Remove((s.Length - "</job title>".Length), "</job title>".Length);
linia = linia + "<c:JobTitle>" + this.usun_specjalne(s) + "</c:JobTitle>";
}
if (s.Contains("<company>"))
{
s = s.Remove(0, "<company>".Length);
s = s.Remove((s.Length - "</company>".Length), "</company>".Length);
linia = linia + "<c:Company>" + this.usun_specjalne(s) + "</c:Company>";
linia = linia + "<c:LabelCollection><c:Label>Business</c:Label></c:LabelCollection></c:Position></c:PositionCollection><c:PersonCollection><c:Person c:ElementID=\"851be0c2-4038-4fed-bf5d-ab8729422b13\"><c:FormattedName>partner</c:FormattedName><c:LabelCollection><c:Label>wab:Spouse</c:Label></c:LabelCollection></c:Person></c:PersonCollection><c:DateCollection><c:Date c:ElementID=\"05915145-a43e-451d-b50f-de202322cb8a\"><c:Value>2011-11-02T12:36:28Z</c:Value><c:LabelCollection><c:Label>wab:Anniversary</c:Label></c:LabelCollection></c:Date><c:Date c:ElementID=\"e59b1ac2-e32d-41ce-a2cb-15554e3ae836\"><c:Value>2011-11-02T12:36:28Z</c:Value><c:LabelCollection><c:Label>wab:Birthday</c:Label></c:LabelCollection></c:Date></c:DateCollection><c:PhotoCollection><c:Photo c:ElementID=\"436904ec-84aa-4e57-9577-b687349dd06a\"><c:LabelCollection><c:Label>UserTile</c:Label></c:LabelCollection></c:Photo></c:PhotoCollection></c:contact>";
}
if (s.Contains("</record>"))
{
list.Add(linia);
System.IO.StreamWriter sw = new System.IO.StreamWriter(id + ".contact", false,Encoding.UTF8);
foreach (String str in list)
{
sw.WriteLine(str);
}
sw.Close();
list.Clear();
id++;
}
}
sr.Close();
// sw.Close();
MessageBox.Show(" zakończono kontakty");
}
}
}
}
}
Brak komentarzy:
Prześlij komentarz