I'm trying to create a calendar invite and wants to add HTML content inside the attached ICS file.
The following code doesn't work:-
private static byte[] CreateiCal(int current_sequence, string guid, string subject, string location, DateTime startTime, DateTime endTime)
{
var a = new StringBuilder();
var sb = new StringBuilder();
a.Append("BEGIN:VCALENDAR\r\f");
a.Append("VERSION:2.0\r\f");
a.Append("PRODID:-//ince/events//NONSGML v1.0//EN\r\f");
a.Append("TZ:+00\r\f");
a.Append("BEGIN:VEVENT\r\f");
a.Append(String.Format("SEQUENCE:{0}\r\f", sequence.ToString()));
a.Append(String.Format("DTSTART:{0}\r\f", GetFormatedDate(startTime)));
a.Append(String.Format("DTEND:{0}\r\f", GetFormatedDate(endTime)));
a.Append(String.Format("LOCATION:{0}\r\f", location));
a.Append(String.Format("UID:{0}\r\f", guid));
a.Append(String.Format("SUMMARY:{0}\r\f", subject));
sb.Append("Sample Text1 \n");
sb.Append("Sample Text2 \n");
sb.Append(string.Format("Sample Text3 <a href='{0}'>LINK</a> \n", "www.google.com"));
sb.Append("Sample Text4 \n");
a.Append(String.Format("DESCRIPTION;X-ALT-DESC;FMTTYPE=text/html:"+sb.ToString() + "\r\f"));
a.Append((string.Format("ORGANIZER:MAILTO:{0}\r\f", "mailID#corporate.com")));
a.Append((string.Format("ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=FALSE;X-NUM-GUESTS=0:mailto:{0}\r\f", "mailID2#corporate.com")));
a.Append(String.Format("X-MICROSOFT-CDO-APPT-SEQUENCE:{0}\r\f", current_sequence.ToString()));
a.Append("BEGIN:VALARM\r\f");
a.Append("TRIGGER:-PT15M\r\f");
a.Append("REPEAT:2\r\f");
a.Append("DURATION:PT15M\r\f");
a.Append("ACTION:DISPLAY\r\f");
a.Append("DESCRIPTION:Reminder\r\f");
a.Append("END:VALARM\r\f");
a.Append("END:VEVENT\r\f");
a.Append("END:VCALENDAR\r\f");
byte[] b = Encoding.ASCII.GetBytes(a.ToString());
return b;
}
Can anyone please share some inputs on how to achieve this?
Thanks in advance.
I finally managed to resolved this.
Please find below the solution:-
private static byte[] CreateiCal(int current_sequence, string guid, string subject, string location, DateTime startTime, DateTime endTime)
{
var a = new StringBuilder();
int sequence = current_sequence + 1;
a.Append("BEGIN:VCALENDAR\r\f");
a.Append("VERSION:2.0\r\f");
a.Append("PRODID:-//ince/events//NONSGML v1.0//EN\r\f");
a.Append("TZ:+00\r\f");
a.Append("BEGIN:VEVENT\r\f");
a.Append(String.Format("SEQUENCE:{0}\r\f", sequence.ToString()));
a.Append(String.Format("DTSTART:{0}\r\f", GetFormatedDate(startTime)));
a.Append(String.Format("DTEND:{0}\r\f", GetFormatedDate(endTime)));
a.Append(String.Format("LOCATION:{0}\r\f", location));
a.Append(String.Format("UID:{0}\r\f", guid));
a.Append(String.Format("SUMMARY:{0}\r\f", subject));
string desc = File.ReadAllText("Sample.html", Encoding.GetEncoding("iso-8859-1")); // iso-8859-1 : HTML contains French characters
a.Append("X-ALT-DESC;FMTTYPE=text/html:" + desc + "\r\f");
a.Append(String.Format("DESCRIPTION:{0}\r\f", desc));
a.Append(String.Format("X-MICROSOFT-CDO-APPT-SEQUENCE:{0}\r\f", current_sequence.ToString()));
a.Append("BEGIN:VALARM\r\f");
a.Append("TRIGGER:-PT15M\r\f");
a.Append("REPEAT:2\r\f");
a.Append("DURATION:PT15M\r\f");
a.Append("ACTION:DISPLAY\r\f");
a.Append("DESCRIPTION:Reminder\r\f");
a.Append("END:VALARM\r\f");
a.Append("END:VEVENT\r\f");
a.Append("END:VCALENDAR\r\f");
byte[] b = Encoding.UTF8.GetBytes(a.ToString());
return b;
}
NOTE:- I've to minify the HTML before Saving it in the "Sample.html" file.
Hope it helps others!!
Related
I am trying to create an outlook appointment ICS file using C# Asp.net. Below is a sample code that I got from the internet, but it is missing Subject, and also attendees. How can I include that in the code? For example Meeting Subject is: "Finance meeting" Attendees is: Sam#mycompany.com, Andy#mycompany.com, Lisa#mycompany.com
public string MakeHourEvent(string subject, string location, DateTime date, string startTime, string endTime)
{
string filePath = string.Empty;
string path = HttpContext.Current.Server.MapPath(#"\iCal\");
filePath = path + subject + ".ics";
writer = new StreamWriter(filePath);
writer.WriteLine("BEGIN:VCALENDAR");
writer.WriteLine("VERSION:2.0");
writer.WriteLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
writer.WriteLine("BEGIN:VEVENT");
string startDateTime = GetFormatedDate(date)+"T"+GetFormattedTime(startTime);
string endDateTime = GetFormatedDate(date) + "T" + GetFormattedTime(endTime);
writer.WriteLine("DTSTART:" + startDateTime);
writer.WriteLine("DTEND:" + endDateTime);
writer.WriteLine("SUMMARY:" + subject);
writer.WriteLine("LOCATION:" + location);
writer.WriteLine("END:VEVENT");
writer.WriteLine("END:VCALENDAR");
writer.Close();
return filePath;
}
The subject is in the SUMMARY: parameter. DESCRIPTION: is used for the "body".
Attendees are added using ATTENDEE: property, i.e.
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;RSVP=TRUE;CN="John Smith"
;X-NUM-GUESTS=0:mailto:john.smith#domain.com
I want to delete the values inside the tags, how can I do that? the highlighted output is what I want to delete when I load the save the file.
<data name="Enrolment_Exit_Verify_Message" d2p1:space="preserve" xmlns:d2p1="http://www.w3.org/XML/1998/namespace">
<value**>**Are you sure you want to Exit without Saving?****</value>
<comment>[Font]**Italic**[/Font][DateStamp]2015/02/01 00:00:00[/DateStamp][Comment] **this is a My awesome new comments comment.!!**[/Comment]</comment>
Here is how I have read in between the tags? I don't know how can I delete in-between the tags.
for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
{
string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
string font = Between(comment, "[Font]", "[/Font]");
string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
string commentVal = Between(comment, "[Comment]", "[/Comment]");
string[] row = new string[]
{
oDataSet.Tables["data"].Rows[i][0].ToString(),
oDataSet.Tables["data"].Rows[i][1].ToString(),
font,
datestamp, commentVal };
Gridview_Output.Rows.Add(row);
}
oDataSet.Tables.Add(oDataTable);
string function
public string Between(string STR, string FirstString, string LastString)
{
string FinalString;
int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
int Pos2 = STR.IndexOf(LastString);
FinalString = STR.Substring(Pos1, Pos2 - Pos1);
return FinalString;
}
You can go with the regex in your between method if you can build up the regex. Here is more info on the regex
public string Between(string STR, string FirstString, string LastString)
{
string regularExpressionPattern1 = #"(?:\" + FirstString + #")([^[]+)\[\/" + LastString;
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
MatchCollection collection = regex.Matches(STR.ToString());
var val = string.Empty;
foreach (Match m in collection)
{
val = m.Groups[1].Value;
}
return val;
}
Note- Code is not tested may need to tweak for your need . Althoguh regex expression is tested.
Here is working fiddle for regex
The above code give you the values from the tags in you r case after executing the functions the variables will have the values
font = "**Italic**"
datestamp = "2015/02/01 00:00:00"
commentVal = "**this is a My awesome new comments comment.!!**"
Then if you want it to remove from the comment variable just use Replaceas
comment = comment.Replace(font,string.Empty);
comment = comment.Replace(datestamp ,string.Empty);
comment = comment.Replace(commentVal ,string.Empty);
At the end of it you will have the comment variable with removed values from that tags.
To delete the values in between you can modify the regular expression provided by #Coder of Code as shown below:
string str = "[Font]**Italic**[/Font][DateStamp]2015/02/01 00:00:00[/DateStamp][Comment] **this is a My awesome new comments comment.!!**[/Comment]";
string strPattern = #"(?:])([^[]+)\["; // Regular expression pattern to find string inside ][
Regex rg = new Regex(strPattern);
string newStr = rg.Replace(str, string.Format("]{0}[",string.Empty), int.MaxValue);
I am programatically trying to POST to a web-service. The issue I face is with my data that I post.
Despite
string post_data = "man=HE&game=01&&address=123 Main St.&cap=1,2,3,4";
new ASCIIEncoding().GetBytes( post_data )
it is not getting converted to
man=HE&game=01&&address=123+Main+St.&cap=1%2C2%2C3%2C4
What is the best way to resolve this?
You are only getting a byte stream that way. In order to URL encode the string you could use the URLEncode method of the HttpUtility helper class:
string post_data = "man=HE&game=01&&address=123 Main St.&cap=1,2,3,4";
string[] postTokens = post_data.Split(new Char [] {'&'});
for(int i = 0; i < postTokens.Length; i++)
{
int pos = postTokens[i].IntexOf("=");
string name = postTokens[i].Substring(0, pos);
string value = postTokens[i].Substring(pos + 1);
postTokens[i] = String.Format("{0}={1}", name, HttpUtility.UrlEncode(value));
}
string encodedPostData = String.Join("=", postTokens);
var encodedPostDataBytes = ASCIIEncoding.GetBytes(encodedPostData);
I think you're confusing ascii encoding with url encoding.
You'll want to use System.Web.HttpServerUtility.UrlEncode method and encode each element of the query string separately.
string post_data =
"man=" + HttpUtility.UrlEncode("HE") +
"&game=" + HttpUtility.UrlEncode("01") // and so forth
When i program,i want to delete a specify string in a long string.
For example:
The source string is:
abcdffff<fdfs>adbcccc
abcdffff<fdferfefs>adbcccc
abcdffff<fdffefes>adbcccc
abcdffff<fdffefefs>adbcccc
The i want to delete the string like <fdfs>
The result should be:
abcdffffadbcccc
abcdffffadbcccc
abcdffffadbcccc
abcdffffadbcccc
How could i do?
This is my code:
public string formatMailMessageBody(string herf,string notifyinfo)
{
StringBuilder sb = new StringBuilder();
sb.Append(notifyinfo.Replace("〈%〉", "") + "<br><br>");
sb.Append("单击下面的链接查看您当前任务:<br>");
sb.Append("<a href='" + herf + "'><b>" + herf + "</b></a>");
string s = sb.ToString();
return sb.ToString();
}
Is it right?
Note that the following code is applicable only if the string you want to delete has this format <...> (no other pairs of <> inside):
var output = Regex.Replace(input, #"\<[^>]*\>", "");
The Regex class is located in the namespace System.Text.RegularExpressions.
For example i have this:
"Was? Wo war ich? Ach ja.<pa>">
I need to create a new text file that will contain only:
Was? Wo war ich? Ach ja.
And i have a big file like 43mb and i need to scan all over the file and get only the places that start with " and end with <pa>" and to get the string between this tags.
I did this code so far:
private void retrivingTestText()
{
w = new StreamWriter(retrivedTextFile);
string startTag = "\"";
string endTag = "<pa>";
int startTagWidth = startTag.Length;
int endTagWidth = endTag.Length;
string text = "\"Was? Wo war ich? Ach ja.<pa>\">";
int begin = text.IndexOf(startTag);
int end = text.IndexOf(endTag, begin + 1);
string result = text.Substring(begin+1, end-1);
w.WriteLine(result);
w.Close();
}
But now i need to make it on a big file 43mb xml file.
So in the constructor i already did StreamReader r;
And string f;
Then i did :
r = new StreamReader(#"D:\New folder (22)\000004aa.xml")
f = r.ReadToEnd();
Now i need to use it with the code above to extract all the strings in the big file between the startTag and endTag and not only specific text.
Second thing i need to make another function so after i make changes it will know to add back all the extractes text strings to the right places where it was before between the startTag and the endTag
Thanks.
You can go for following approach to extract the data.
string word = "\"Was? Wo war ich? Ach ja<pa>\"Jain\"Romil<pa>\"";
string[] stringSeparators = new string[] { "<pa>\"" };
string ans=String.Empty;
string[] text = word.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in text)
{
if (s.IndexOf("\"") >= 0)
{
ans += s.Substring(s.IndexOf("\"")+1);
}
}
return ans;
There is a similar post on how to remove HTML tags using regular expressions. Here is the link.
And another one that you can tweak, here.