自己写的 C# 解晰VCARD 的类 简单的VCARD
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
namespace VCARD
{
public class VCard
{
/// <summary>
/// 组结构
/// </summary>
public struct Group
{
public string N;//组名称
public string UID;//组ID
public string TYPE;//类别
public string TYPE1;//系统组使用的
}
/// <summary>
/// 邮件结构
/// </summary>
public struct Email
{
/// <summary>
/// 家庭地址
/// </summary>
public string ADR_HOME;
/// <summary>
/// 公司地址
/// </summary>
public string ADR_WORK;
/// <summary>
/// 生日
/// </summary>
public string BDAY;
/// <summary>
/// 邮件地址
/// </summary>
public string EMAIL_PREF;
/// <summary>
/// 名字
/// </summary>
public string FN;
/// <summary>
/// 所属分组
/// </summary>
public string Grouping;
/// <summary>
/// 即时通讯
/// </summary>
public string ICQ;
/// <summary>
/// 公司名字
/// </summary>
public string ORGNAME;
/// <summary>
/// 家,邮政编码;
/// </summary>
public string PC_HOME;
/// <summary>
/// 公司,邮政编码
/// </summary>
public string PC_WORK;
/// <summary>
/// 手机
/// </summary>
public string TEL_CELL;
/// <summary>
/// 家庭电话
/// </summary>
public string TEL_HOME_VOICE;
/// <summary>
/// 公司,传真号码
/// </summary>
public string TEL_WORK_FAX;
/// <summary>
/// 公司,电话号码
/// </summary>
public string TEL_WORK_VOICE;
/// <summary>
/// ID
/// </summary>
public string UID;
/// <summary>
/// 个人,主页
/// </summary>
public string URL;
/// <summary>
/// 备注
/// </summary>
public string ZS;
}
private Email[] emails = { };
public Email[] Emails
{
get { return emails; }
set { emails = value; }
}
private Group[] groups ={ };
public Group[] Groups
{
get { return groups; }
set { groups = value; }
}
/// <summary>
/// 组解析
/// </summary>
/// <param name="options"></param>
/// <param name="s"></param>
public void ToGroup(RegexOptions options, string s)
{
Regex regex;
Match m;
MatchCollection mc;
regex = new Regex(@"(\n(?<strElement>(N))) (:(?<strSurname>([^\n]*))) (\n(UID:)(?<strUID>([^\n]*))) (\n(X-TYPE:)(?<strTYPE>([^\n]*))) (\n(X-TYPE1:)(?<strTYPE1>([^\n]*)))?", options);
mc = regex.Matches(s);
if (mc.Count <= 0)
return;
Groups = new Group[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
Groups[i].N = m.Groups["strSurname"].Value;
Groups[i].UID = m.Groups["strUID"].Value;
Groups[i].TYPE = m.Groups["strTYPE"].Value;
Groups[i].TYPE1 = m.Groups["strTYPE1"].Value;
}
}
/// <summary>
/// 邮件地址解析
/// </summary>
/// <param name="options"></param>
/// <param name="s"></param>
public void ToEmails(RegexOptions options, string s)
{
Regex regex;
Match m;
MatchCollection mc;
string gex = "((\nADR;HOME:)(?<ADR_HOME>([^\n]*)))?";
gex += "((\nADR;WORK:)(?<ADR_WORK>([^\n]*)))?";
gex += "((\nBDAY:)(?<BDAY>([^\n]*)))?";
gex += "(\n(?<strElement>(EMAIL)))";
gex += "((;PREF:)(?<EMAIL_PREF>([^\n]*)))";
gex += "((\nFN:)(?<FN>([^\n]*)))";
gex += "((\nGrouping:)(?<Grouping>([^\n]*)))";
gex += "((\nICQ:)(?<ICQ>([^\n]*)))?";
gex += "((\nORGNAME:)(?<ORGNAME>([^\n]*)))?";
gex += "((\nPC;HOME:)(?<PC_HOME>([^\n]*)))?";
gex += "((\nPC;WORK:)(?<PC_WORK>([^\n]*)))?";
gex += "((\nTEL;CELL:)(?<TEL_CELL>([^\n]*)))?";
gex += "((\nTEL;HOME;VOICE:)(?<TEL_HOME_VOICE>([^\n]*)))?";
gex += "((\nTEL;WORK;FAX:)(?<TEL_WORK_FAX>([^\n]*)))?";
gex += "((\nTEL;WORK;VOICE:)(?<TEL_WORK_VOICE>([^\n]*)))?";
gex += "((\nUID:)(?<UID>([^\n]*)))?";
gex += "((\nURL:)(?<URL>([^\n]*)))?";
gex += "((\nZS:)(?<ZS>([^\n]*)))?";
regex = new Regex(gex, options);
mc = regex.Matches(s);
if (mc.Count > 0)
{
Emails = new Email[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
m = mc[i];
Emails[i].ADR_HOME = m.Groups["ADR_HOME"].Value;
Emails[i].ADR_WORK = m.Groups["ADR_WORK"].Value;
Emails[i].BDAY = m.Groups["BDAY"].Value;
Emails[i].EMAIL_PREF = m.Groups["EMAIL_PREF"].Value;
Emails[i].FN = m.Groups["FN"].Value;
Emails[i].Grouping = m.Groups["Grouping"].Value.Replace(';', '|');
Emails[i].ICQ = m.Groups["ICQ"].Value;
Emails[i].ORGNAME = m.Groups["ORGNAME"].Value;
Emails[i].PC_HOME = m.Groups["PC_HOME"].Value;
Emails[i].PC_WORK = m.Groups["PC_WORK"].Value;
Emails[i].TEL_CELL = m.Groups["TEL_CELL"].Value;
Emails[i].TEL_HOME_VOICE = m.Groups["TEL_HOME_VOICE"].Value;
Emails[i].TEL_WORK_FAX = m.Groups["TEL_WORK_FAX"].Value;
Emails[i].TEL_WORK_VOICE = m.Groups["TEL_WORK_VOICE"].Value;
Emails[i].UID = m.Groups["UID"].Value;
Emails[i].URL = m.Groups["URL"].Value;
Emails[i].ZS = m.Groups["ZS"].Value;
}
}
}
/// <summary>
/// 格式化成XML
/// </summary>
/// <returns></returns>
public override string ToString()
{
string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
str += "<vCard>\n";
string str2;
foreach (Email var in emails)
{
str2 = "<Email";
str2 += " ADR_HOME=\"" + var.ADR_HOME.Trim() + "\" ";
str2 += " ADR_WORK=\"" + var.ADR_WORK.Trim() + "\"";
str2 += " BDAY=\"" + var.BDAY.Trim() + "\"";
str2 += " EMAIL_PREF=\"" + var.EMAIL_PREF.Trim() + "\"";
str2 += " FN=\"" + var.FN.Trim() + "\"";
str2 += " Grouping=\"" + var.Grouping.Trim() + "\"";
str2 += " ICQ=\"" + var.ICQ.Trim() + "\"";
str2 += " ORGNAME=\"" + var.ORGNAME.Trim() + "\"";
str2 += " PC_HOME=\"" + var.PC_HOME.Trim() + "\"";
str2 += " PC_WORK=\"" + var.PC_WORK.Trim() + "\"";
str2 += " TEL_CELL=\"" + var.TEL_CELL.Trim() + "\"";
str2 += " TEL_HOME_VOICE=\"" + var.TEL_HOME_VOICE.Trim() + "\"";
str2 += " TEL_WORK_FAX=\"" + var.TEL_WORK_FAX.Trim() + "\"";
str2 += " TEL_WORK_VOICE=\"" + var.TEL_WORK_VOICE.Trim() + "\"";
str2 += " UID=\"" + var.UID.Trim() + "\"";
str2 += " URL=\"" + var.URL.Trim() + "\"";
str2 += " ZS=\"" + var.ZS.Trim() + "\"";
str2 += "></Email>";
str += str2.Replace("\n", "");
str += "\n";
}
foreach (Group var in groups)
{
str2 = "<Group";
str2 += " N=\"" + var.N.Trim() + "\"";
str2 += " TYPE=\"" + var.TYPE.Trim() + "\"";
str2 += " TYPE1=\"" + var.TYPE1.Trim() + "\"";
str2 += " UID=\"" + var.UID.Trim() + "\"";
str2 += "></Group>";
str += str2.Replace("\n", "");
str += "\n";
}
str += "</vCard>";
return str;
}
/// <summary>
/// 分析Vcard
/// </summary>
public void ParseLines(string s)
{
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
ToGroup(options, s);
ToEmails(options, s);
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public string Encode(DataSet ds)
{
string str = "iaddress=BEGIN:VCARD\r\n";
str += "VERSION:2.1\r\n";
str += "X-TEBIE WORK GROUP{G_N}\n";//:0:1:FRIENDS:FAMILY:COWORKERS:NETFRIENDS:VIP:udef_20080904114523
string G_N = "";
string str2;
foreach (DataRow dr in ds.Tables[0].Rows)
{
str2 = "BEGIN:VCARD\r\n";
str2 += "BDAY:19861030\r\n";
str2 += "EMAIL;PREF:" + dr["Address"].ToString() + "\r\n";
str2 += "FN:" + dr["Name"].ToString() + "\r\n";
str2 += "Grouping:" + dr["GroupID"].ToString().Replace('|', ';') + "\r\n";
str2 += "UID:" + dr["UID"] + "\r\n";
str2 += "END:VCARD";
str += str2;
str += "\r\n";
G_N += ":" + dr["UID"];
}
foreach (DataRow dr in ds.Tables[1].Rows)
{
str2 = "BEGIN:VCARD" + "\r\n";
str2 += "N:" + dr["Name"].ToString() + "\r\n";
str2 += "UID:" + dr["UID"].ToString() + "\r\n";
str2 += "X-TYPE:" + dr["Type"].ToString() + "\r\n";
if (dr["Type1"].ToString() != "")
str2 += "X-TYPE1:SYSTEM" + "\r\n";
str2 += "END:VCARD";
str += str2;
str += "\r\n";
G_N += ":" + dr["UID"];
}
str += "END:VCARD\r\n";
return str.Replace("{G_N}", G_N);
}
}
}
来源:https://www.cnblogs.com/lystory/archive/2008/09/25/1298960.html