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);
I have to do if user's browser compatibility is on then need to show message to user that your browser's compatibility is on.
I have searched this a lot on google but yet not found a proper answer.
I have tried below code but HttpContext.Current.Request.UserAgent always contains MSIE 7.0
string isOn = string.Empty;
if (HttpContext.Current.Request.UserAgent.IndexOf("MSIE 7.0") > -1)
{
isOn = "IE8 Compatibility View";`
}
else
{
isOn = "IE8";
}
}
You may try like this
if (Request.Browser.Type.ToUpper().Contains("IE"))
{
if (Request.Browser.MajorVersion < 7)
{
//Show the message here
}
...
}
else if (Request.Browser.Type.Contains("Firefox"))
{
//code to show message
}
else if (Request.Browser.Type.Contains("Chrome"))
{
//code to show message
}
Also check this MSDN which has its own way of detecting the browser
Query the Browser property, which contains an HttpBrowserCapabilities
object. This object gets information from the browser or client device
during an HTTP request, telling your application the type and level of
support the browser or client device offers. The object in turn
exposes information about browser capabilities using strongly typed
properties and a generic name-value dictionary.
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
TextBox1.Text = s;
}
I'm trying to update a file via the GitHub API.
I've got everything setup, and in the end the actual file is updated, which is a good thing.
However, let's say I've got these 2 files in my repo
FileToBeUpdated.txt
README.md
And then I run my program
The FileToBeUpdated.txt is updated, like it should, however the README.md is deleted.
This is the code with the 5 steps to update the file:
private static void Main()
{
string shaForLatestCommit = GetSHAForLatestCommit();
string shaBaseTree = GetShaBaseTree(shaForLatestCommit);
string shaNewTree = CreateTree(shaBaseTree);
string shaNewCommit = CreateCommit(shaForLatestCommit, shaNewTree);
SetHeadPointer(shaNewCommit);
}
private static void SetHeadPointer(string shaNewCommit)
{
WebClient webClient = GetMeAFreshWebClient();
string contents = "{" +
"\"sha\":" + "\"" + shaNewCommit + "\", " +
// "\"force\":" + "\"true\"" +
"}";
// TODO validate ?
string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "refs/heads/master", "PATCH", contents);
}
private static string CreateCommit(string latestCommit, string shaNewTree)
{
WebClient webClient = GetMeAFreshWebClient();
string contents = "{" +
"\"parents\" :[ \"" + latestCommit + "\" ], " +
"\"tree\":" + "\"" + shaNewTree + "\", " +
"\"message\":" + "\"test\", " +
"\"author\": { \"name\": \""+ Constants.Username +"\", \"email\": \""+ Constants.Email+"\",\"date\": \"" + DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture) + "\"}" +
"}";
string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "commits", contents);
var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);
return foo.sha;
}
private static string CreateTree(string shaBaseTree)
{
WebClient webClient = GetMeAFreshWebClient();
string contents = "{" +
"\"tree\" :" +
"[ {" +
"\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," +
"\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
"\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
"} ]" +
"}";
string downloadString = webClient.UploadString(Constants.Start + Constants.PathToRepo + "trees", contents);
var foo = JsonConvert.DeserializeObject<TreeRootObject>(downloadString);
return foo.sha;
}
private static string GetShaBaseTree(string latestCommit)
{
WebClient webClient = GetMeAFreshWebClient();
string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "commits/" + latestCommit);
var foo = JsonConvert.DeserializeObject<CommitRootObject>(downloadString);
return foo.tree.sha;
}
private static string GetSHAForLatestCommit()
{
WebClient webClient = GetMeAFreshWebClient();
string downloadString = webClient.DownloadString(Constants.Start + Constants.PathToRepo + "refs/heads/master");
var foo = JsonConvert.DeserializeObject<HeadRootObject>(downloadString);
return foo.#object.sha;
}
private static WebClient GetMeAFreshWebClient()
{
var webClient = new WebClient();
webClient.Headers.Add(string.Format("Authorization: token {0}", Constants.OAuthToken));
return webClient;
}
Which part am I missing? Am I using the wrong tree to start from?
It seems that in the function CreateTree I should set the base_tree at the root level, not at the level of each tree I create.
So in the function CreateTree the contents become this:
string contents = "{" +
"\"base_tree\": " + "\"" + shaBaseTree + "\"" + "," + // IT'S THIS LINE!
"\"tree\" :" +
"[ {" +
"\"path\": " + "\"" + Constants.FileToChange + "\"" + " ," +
"\"content\":" + "\"" + DateTime.Now.ToLongTimeString() + "\"" +
"} ]" +
"}";
Updated GitHub documentation: https://github.com/Snakiej/developer.github.com/commit/2c4f93003703f68c4c8a436df8cf18e615e293c7
I have a website that makes request to our page on http://www.xyz.com/test.aspx and in return we have to post back the response. Following code is what is sending the response back. Problem is it sends the XML data back but with it also it sends the code of test.aspx
I don't know how to remove that.
Random random = new Random();
int randomNumber = random.Next(0, 10000000);
attrval = randomNumber + "tty";
strCXML = "<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + " UTF-8" + "\"" + " ?>";
strCXML =strCXML+"<!DOCTYPE cXML SYSTEM " + "\"" + "http://xml.cxml.org/schemas/cXML/1.2.023/cXML.dtd" + "\"" + ">";
strCXML = strCXML + " <cXML payloadID=" + "\"" + attrval + "\"" + " timestamp=" + "\"" + strTimeStamp + "\"" + ">";
strCXML = strCXML + "<Response>";
strCXML = strCXML + " <Status code=" + "\"" + 200 + "\"" + " text=" + "\"" + "success" + "\"" + ">" + "</Status>";
strCXML = strCXML + " <PunchOutSetupResponse> ";
strCXML = strCXML + " <StartPage>";
strCXML = strCXML + " <URL>" + strMySiteURL + "</URL>";
strCXML = strCXML + " </StartPage>";
strCXML = strCXML + " </PunchOutSetupResponse>";
strCXML = strCXML + " </Response>";
strCXML = strCXML + "</cXML>";
Response.Write(strCXML);
This is working fine except that when it posts the data back it also is including the html of test.aspx page
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
</body>
</html>
Please contact support with the Error Reference Number: ANERR-10000000000000000057040121 for more details</Status>
</Response>
</cXML>
Any ideas how I post it back so it only posts back the XML data above.
You need to call Response.End after you write the xml to stream, otherwise the site will continue to render the page.
I have the following condition using in my code but that doesn't look very efficient,is this any better way to handle ?
if (ic = filename.Contains(".wmv"))
{
if (bitnumber > 400)
{
path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
UploadCondition(path, filename);
//return path;
}
}
if (ic = filename.Contains(".wmv"))
{
if (bitnumber < 400)
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
//return path;
}
}
if (ic = filename.Contains(".m4v"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".mp4"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".flv"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".mpg"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".aac"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
if (ic = filename.Contains(".mp3"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
Break it up in other classes like:
public class AudioFileValidator
{
private List<string> _extensions = new List<string>{".aac", ".mp3"};
public bool IsValid(string filename)
{
if (!_extensions.Contains(Path.GetExtension(filename))
return false;
//validate bitrate etc
}
}
Usage:
var audioValidator = new AudioFileValidator();
if (audioValidator.IsValid(filename))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
var videoValidator = new VideoFileValidator();
if (videoValidator.IsValid(filename))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
By doing so you'll get classes with a single responsibility which can be reused in other places and which are easy to unit test.
You could even take it further and introduce a new interface called IMediaFileValidator which all validators implement. and do something like:
foreach (var validator in validators)
{
if (validator.IsValid(filename))
{
// use info from the validator to build the path
var mediaName = validator.MediaName;
path = "ftp://" + ftpServerIP + "/" + mediaName + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
break;
}
}
Which would also make your code adhere to Open/Closed principle.
You will need a lot of refactoring. Here are couple of ideas to get you started:
Use String.Format and passed in only value that changed to save you all the repeating the text
Build a dictionary of Extension/Ext-Combination key and set the value to the the destination path. You will then only require one lookup than big nesting if - else statements
Use Path.GetExtension rather than Contains to be more accurate
Eg.
string formatStringNews = "ftp://{0}/news/{1}/";
string formatStringMedia = "ftp://{0}/media/{1}/";
dictionary["wmv"] = formatStringMedia;
dictionary["mp3"] = formatStringNews;
....
string key = Path.GetExtension(filename);
path = string.Format(dictionary[key], serverName, programName);
Something like this is a nice short solution to your problem and I believe it handles all of the cases your if statements are handling.
String[] videoExtensions = { "wmv", "m4v", "mp4", "flv" };
String[] audioExtensions = { "aac", "mp3" };
String ext = Path.GetExtension(filename).ToLower();
String path = "ftp://" + ftpServerIP + "/";
if (-1 != Array.IndexOf(videoExtensions, ext)) {
if ("wmv".equals(ext) && bitnumber > 400)
path += "media/lib/" + programName + "/" + date + "/";
else
path += "mpegmedia/news/" + programName + "/video/podcast/";
}
else if (-1 != Array.IndexOf(audioExtensions, ext)) {
path += "mpegmedia/news/" + programName + "/audio/podcast/";
}
else {
// handle unknown extension types as desired
}
UploadCondition(path, filename);
Use a switch statement and System.IO.Path.GetExtension.
select (System.IO.Path.GetExtension(filename))
{
case ".wmv":
if (bitnumber > 400)
{
path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
UploadCondition(path, filename);
//return path;
}
else
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
//return path;
}
break;
case ".m4v":
case ".mp4":
case ".flv":
case ".mpg":
case ".mp3":
default:
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
break;
}
}
I'm guessing you'll want variations for the last block, but this should be easy enough to modify.
At the very least you could convert it into an if/elseif statement:
if (ic....)
{
...
} else if (ic...) {
...
}
I assume, at a time your filename will be either be .m4v, .flv,.mp4 etc...so here goes the code..
if (ic = filename.Contains(".wmv"))
{
if (bitnumber > 400)
{
path = "ftp://" + ftpServerIP + "/" + "media" + "/" + "lib" + "/" + programName + "/" + date + "/";
UploadCondition(path, filename);
//return path;
}
else
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
//return path;
}
}
else if (ic = filename.Contains(".m4v"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else if (ic = filename.Contains(".mp4"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else if (ic = filename.Contains(".flv"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else if (ic = filename.Contains(".mpg"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "video" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else if (ic = filename.Contains(".aac"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else if (ic = filename.Contains(".mp3"))
{
path = "ftp://" + ftpServerIP + "/" + "mpegmedia" + "/" + "news" + "/" + programName + "/" + "audio" + "/" + "podcast" + "/";
UploadCondition(path, filename);
}
else
{
//No Match found
}
and the best approach would be to use Switch(fileExtn)
You could perhaps make it a bit simpler like
if (filename.Contains(".wmv"))
// path = set the path as you require
and after all the ifs end call your method
UploadCondition(path, filename);
Better would be to extract the extension .wmv, .m4v from filename and make this into a switch whereby you set the path.