I am trying to get a snippet of HTML between to comments.
I will need to parse the HTML between the start/end later.
I am actually reading from an html file but for test purposes I mocked the following up:
string emailFeedTxtStart = "<!--FEED FOR RECEIPT GOES HERE-->";
string emailFeedTxtEnd = "<!--FEED FOR RECEIPT ENDS HERE-->";
string html =
emailFeedTxtStart + Environment.NewLine +
#"<td align=""center"">" + Environment.NewLine +
#"<table style=""table-layout:fixed;width:380px"" border=""0"" cellspacing=""0"" cellpadding=""0"">" + Environment.NewLine +
"<tbody>" + Environment.NewLine +
"<tr>" + Environment.NewLine +
"<td>" + Environment.NewLine +
"</td>" + Environment.NewLine +
"</tr>" + Environment.NewLine +
"</tbody>" + Environment.NewLine +
"</table>" + Environment.NewLine +
"</td>" + Environment.NewLine +
emailFeedTxtEnd;
string patternstart = Regex.Escape(emailFeedTxtStart);
string patternend = Regex.Escape(emailFeedTxtEnd);
string regexexpr = patternstart + #"(.*?)" + patternend;
//string regexexpr = #"(?<=" + patternstart + ")(.*?)(?=" + patternend + ")";
MatchCollection matches = Regex.Matches(#html, #regexexpr);
matches returned is 0.
(note there is a lot more HTML between the ).
Any help would be greatly appreciated.
What are you going to parse the HTML with after? Because there's probably a way you can just do away with actually manipulating the HTML string beforehand. Here's a solution anyway:
string afterFirst = html.Substring(Regex.Match(html, emailFeedTxtStart).Index + emailFeedTxtStart.Length);
string between = afterFirst.Substring(0, Regex.Match(afterFirst, emailFeedTxtEnd).Index);
Related
In my application, I want to add a header and footer to a word document and download it. So I used to create HTML content and convert that content to a word document and download it. It is working on my local PC. When I hosted my application, images of the downloaded word document has not appeared.
I think images are saved in the server. when I download the document from the client-side machine, the images can't find the path. I used StreamWriter to convert HTML content to Word.
Please provide me a solution for how to bind an image with the absolute path with a permanent way to a word document through HTML or any other way to convert HTML content to word.
1st way I tried;
string renderHeaderImageLogo1 = Server.MapPath("~/Resources/ImageLogo/letter_header.jpg");
string renderFooterImageLogo1 = Server.MapPath("~/Resources/ImageLogo/letter_footer.jpg");
byte[] imageArrayHeader = System.IO.File.ReadAllBytes(renderHeaderImageLogo1);
byte[] imageArrayFooter = System.IO.File.ReadAllBytes(renderFooterImageLogo1);
string renderHeaderImageLogo = Convert.ToBase64String(imageArrayHeader);
string renderFooterImageLogo = Convert.ToBase64String(imageArrayFooter);
string headerImagePath = string.Empty;
string footerImagePath = string.Empty;
headerImagePath = "<div style=" + '"' + "text-align:" + headerImageAlignLocation + '"' + '"' + ">" + "<img src=" + "\"data:image/png;base64," + renderHeaderImageLogo + '"' + "/>" + "</div>";
footerImagePath = "<div style=" + '"' + "text-align:" + footerImageAlignLocation + '"' + '"' + ">" + "<img src=" + "\"data:image/png;base64," + renderFooterImageLogo + '"' + "/>" + "</div>";
2nd way i tried:
string renderHeaderImageLogo = Server.MapPath("~/Resources/ImageLogo/letter_header.jpg");
string renderFooterImageLogo = Server.MapPath("~/Resources/ImageLogo/letter_footer.jpg");
string headerImagePath = string.Empty;
string footerImagePath = string.Empty;
headerImagePath = "<div style=" + '"' + "text-align:" + headerImageAlignLocation + '"' + '"' + ">" + "<img src=" + '"' + renderHeaderImageLogo + '"' + "/>" + "</div>";
footerImagePath = "<div style=" + '"' + "text-align:" + footerImageAlignLocation + '"' + '"' + ">" + "<img src=" + '"' + renderFooterImageLogo + '"' + "/>" + "</div>";
3rd way I tried. This is not working for me. because I hosted my application as HTTPS
<img src="http://www.website.com/path/to/image.jpg" width="233" height="54" style="width:233pt;height:54pt;" />
This is the way I used to convert HTML content to word. content is the HTML content.
var npath = pathNew + (templateTitle.Replace(" ", "_")) + " (" + emp + "-" + empCallingName + ")" + ".doc";
try
{
using (StreamWriter writetext = new StreamWriter(npath))
{
writetext.WriteLine(content.ToString());
writetext.Close();
}
}
catch (Exception ex)
{
Log.Error(ex);
throw ex;
}
While sent email using below subject which apostrophe replacing with another characters
Actual Subject : We’ll make 100,800 cold calls for you
Mail Shows Subject : We’ll make 100,800 cold calls for you
Issue happens when I'm sent email via api , when sent email from SMTP it's working fine
Please check my api code below
string msg = "From: " + FromName + "<" + From + ">" + " \r\n" +
"To: " + ToName + "<" + To + ">" + " \r\n" +
"BCC: " + BCCEmail + " \r\n" +
"Subject: " + Subject + " \r\n" +
"Message-ID: mID_" + messageID + "\r\n" +
"References: "+encryptMessageID + "\r\n" +
"In-Reply-To: " + encryptMessageID + "\r\n" +
"Content-Type: " + contentType + "; charset=us-ascii\r\n\r\n" + Body;
dynamic objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg) };
if (!string.IsNullOrEmpty(messageThreadID))
objSendMsg = new { raw = commonFunction.Base64UrlEncode(msg), threadId = messageThreadID };
var _objSendMsg = JsonConvert.SerializeObject(objSendMsg);
var strSendMsg = new StringContent(_objSendMsg, UnicodeEncoding.UTF8, "application/json");
When same content i'm applying in body with apostrophe working fine for body
Please check attached screenshot
Email copy
You need to base64_encode of the subject header your sending it as plain text. the API is getting confused.
Subject: " + Convert.ToBase64String(Subject) + " \r\n" +
Starting with an example, I have some keywords like
Narendra Modi, Modi, India, Speech, Parliament
And I have a story with text
Narendra Modi will give a speech in the parliament of India.
Now I want my regex to replace the keyword with the hyperlink in the story.
<a>Narendra Modi</a> will give speech in the <a>parliament</a> of <a>India.</a>
My Code for this is
var tagArray = bodykeywords.Split(',').ToList();
foreach (var tag in tagArray.OrderBy(a => a.Length))
{
var replaceTag = tag.Replace("(", "");
replaceTag = replaceTag.Replace(")", "");
DataDesc = Regex.Replace(DataDesc, "\"" + replaceTag.Trim() + "\"", " \"" + replaceTag.Trim() + "\" ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + ", ", " " + replaceTag.Trim() + ", ");
DataDesc = Regex.Replace(DataDesc, " " + replaceTag.Trim() + " ", " " + replaceTag.Trim() + " ");
}
Problem is I am not able to replace the word with full stop like India in the given example and Word with repetition like Narendra Modi, Modi in the keyword.
I'am exporting some data to a .txt file as follows:
String content;
String path=#"e:\coding\";
String name="test.txt";
path+=name;
System.IO.File.Delete(path);
for (i=0;i<row-1;i++)
{
try
{
if (r[i].points.Count() > 2)
{
content = "Route " + (i + 1).ToString() +" Truck_id:"+trk[i].truck_name.ToString()+ " Max_load="+trk[i].capacity.ToString()+ "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
System.IO.File.AppendAllText(path, "Points Load Reached_AT Max_load" + Environment.NewLine);
System.IO.File.AppendAllText(path, "========================================" + Environment.NewLine);
for (int j = 0; j < (r[i].points.Count()); j++)
{
content = r[i].points[j].ToString() + " " + c[r[i].points[j]].load.ToString() +" "+ r[i].time_list[j].ToString()+" "+c[r[i].points[j]].max_load.ToString()+"\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
}
content = "Total " + r[i].ld.ToString() + "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine );
content = "Route Complete: " + r[i].reach_at.ToString();
System.IO.File.AppendAllText(path, content + Environment.NewLine+Environment.NewLine);
}
}
catch (IndexOutOfRangeException e)
{
break;
}
}
As expected the output I get is not properly formatted:
The spaces are causing the text to be jumbled and not arranged. My reputation does'nt allow me to post a screenshot but I guess It can be understood what is happening.
Is there way so that the text is properly formatted neatly column wise without looking jumbled.
If you need a text, you can use tabs:
System.IO.File.AppendAllText(path, "Points\t\tLoad\t\tReached_AT\t\tMax_load" + Environment.NewLine);
// ...
content = r[i].points[j].ToString() + "\t\t " + c[r[i].points[j]].load.ToString() +"\t\t"+ r[i].time_list[j].ToString()+"\t\t"+c[r[i].points[j]].max_load.ToString()+"\n";
Just play with amount of tabs (\t for one, \t\t for two, etc...). Hope it can help.
Another solution would be to use commas:
System.IO.File.AppendAllText(path, "Points,Load,Reached_AT,Max_load" + Environment.NewLine);
and save to CSV-file (comma-separated values). Then you can import the data to Microsoft Excel or to other software.
You can find bunch full of good information on how to format the string contents in the The format item MSDN but for quick answer, an example for your string
content = "Route " + (i + 1).ToString() + " Truck_id:" + trk[i].truck_name.ToString() + " Max_load=" + trk[i].capacity.ToString() + "\n";
If we assume,
i maximum 10 digits,
Truck_name max 45 characters
capacity max 10 digits
content = String.Format("{0,-20}{1,55}{2,20} " + Environment.NewLine, "Route " + (i + 1).ToString(), " Truck_id:" + trk[i].truck_name.ToString(), " Max_load=" + trk[i].capacity.ToString());
I have the following code for defining XML Schema. Having a problem in keeping the lines together. Worked fine before.
public static FileContentResult WriteTo(SiteMapFeed feedToFormat)
{
var siteMap = feedToFormat;
//TODO: DO something, next codes are just DEMO
var header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ Environment.NewLine + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\""
+ Environment.NewLine + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ Environment.NewLine + "xsi:schemaLocation=\""
+ Environment.NewLine + "http://www.sitemaps.org/schemas/sitemap/0.9"
+ Environment.NewLine + "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
var urls = new System.Text.StringBuilder();
foreach (var site in siteMap.Items)
{
urls.Append(string.Format("<url><loc>http://www.{0}/</loc><lastmod>{1}</lastmod><changefreq>{2}</changefreq><priority>{3}</priority></url>", site.Url, site.LastMod, site.ChangeFreq, site.Priority));
}
byte[] fileContent = System.Text.Encoding.UTF8.GetBytes(header + urls + "</urlset>");
return new FileContentResult(fileContent, "text/xml");
}
SO this is now causing the following error:
Where am I doing it wrong? Thanks
Problem is in that "xsi:schemaLocation" bit I think - you can't have multiple lines between quotes in an XML attribute, if my brain remembers correctly. Try changing to:
var header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ Environment.NewLine + "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\""
+ Environment.NewLine + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ Environment.NewLine + "xsi:schemaLocation=\""
+ "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">" + Environment.NewLine;