Read content from a URL, place into a textbox - c#

I'm trying to read lines from a website and then, copy it into my textBox2.
textBox1 will have a website's URL like http://example.com.
When I click on button1 I'd like to read HTML content from the above URL, and please that info textBox2.
Should I use HtmlAgilityPack?
How can this be done?
2 =================================================
So, for example, i copy this link into my "textBox1"
http://www.mineshaftersquared.com/server/DareCraft#
So, is there a way to make app copy everything from:
//*[#id="Plugins"]
to
//*[#id="rightInfo"]/section[2]/div/div[1]/table[2]
/this is XPath
/It don't have to in XPath, but this was the only way I could show.

You don't install HtmlAgilityPack, you reference if from your project.
Read about WebClient class (OpenRead method), you probably want to use it to get the pages.
Here is a tutorial you might want to start with:
http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient

Related

c# selenium chrome-webdriver write advanced Path (chrome-driver)

I just try to write correct path to click exactly this button what I want on my page. I will give you an example page for testing. You just have to download the HTML file and open it in your browser.Code of HTML page
What we now want to do?
If you run this HTML file you will see all page.
And now we want to make a Click on exactly this button on screen :
After when you click on this button you will see click counter below: like this :
Have anyone idea how to click it? I tried few ways and can't find solution still. Please help.
I try at least :
drive.FindElement(By.XPath("//tr[class='ng-scope']/td[text()='Wylaczenie nadan RDF'] and button[#title='Skip']")).Click();
and
drive.FindElement(By.XPath("//tr[text()='Wylaczenie nadan RDF']/button[#title='Skip']")).Click();
+ more and more and can't just write fine this Path to click exacly this button.
And unique value is <td class="ng-binding">Wylaczenie nadan RDF</td> - i prefer to get path by this.
You need to use below XPATH for the same
//tr[td='Wylaczenie nadan RDF']//button[#title='Skip']"
//tr[td='Wylaczenie nadan RDF'] will take you to the row which contains that text and then you use //button[#title='Skip'] to find that button

Making a file with full path suitable to act as an href in the HTML

I have a piece of code that is like
string.Format("<a href='{0}' class='hidden'>{0}</a>", Path.Combine(filePathPrefix , string.Join("-", new string[] { fOrgName, fCatName, f.filename })))
and meant to return an a tag that a user can click on to get a file. The only problem is that string I'm building for the href does not work as a link. It is something like
c:\users\me\documents\visual studio 2013\Projects\myproj\myproj\Assets\someorg-somecat-somepic.png
which doesn't work as a clickable link, brings to an about:blank page if you click on it. However, if I paste the link into my browser and make the request then it changes to
file:///C:/users/me/documents/visual%20studio%202013/Projects/mypoj/myproj/Assets/someorg-somecat-somepic.png
and brings up the asset properly. That means I need some way getting the link in that form while its on the page. Is this possible? If so, what C# class(es) and method(s) do I need?
Try changing to this:
string.Format("{0}", ...);

How do I create hyperlinks from resource files?

I have some text which is loaded from a resource file. Ordinarily, to get dynamic content I would use:
string.Format(GetLocalResourceObject("SomeText"), PhoneNumberAsString)
I want to do the same with a link, only the link needs to be application relative as I have URLs like mysite.com/page.aspx and mysite.com/fr/page.aspx.
I normally use an <asp:HyperLink /> tag to create the links as I can then just put a squiggle at the start NavigateUrl="~/page.aspx". However, I don't know of a way to get a dynamic HyperLink to appear as a string without adding it as a control to something.
Simply writing ToString() outputs System.Web.UI.WebControls.HyperLink..
How do I get a link out of a resource file and make it into a hyperlink using ASP.NET Webforms?
UPDATE
With some help from the answers I now have the following code on my page:
<p><%= string.Format(GetGlobalResourceObject("Resource", "MoreThan1000Users").ToString(), ResolveUrl("~/contact-us.aspx")) %></p>
and in my resource file I have:
If you would like more than 1000 users please call our sales team.
Does this seem like good practice or is there another way to achieve what I'm doing? I don't know if I should be happy or not that there is HTML inside the resource file.
Since you haven't posted code, I'm guessing somewhere you have a HyperLink WebControl object that you're hitting ToString() on. If that's the case, you can access the URL associated with it using its myHyperLinkControl.NavigateUrl property.
If you're storing the link in your resource with a squiggle/tilde (which is good) then you can replace the squiggle with your application location. If you have a control/page, then you can easily call its ResolveURL method (which takes the tilde and automatically replaces it) There's some existing solutions if you don't have a control/page reference with your context, then there's some discussion to do that here: ResolveUrl without an ASP.NET Page
I guess this is what you want:
Server.MapPath("~/page.aspx")
That will work inside your aspx and your code-behind.

ASP.Net Hyperlink navigation URL not including server name

This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.
You have to set the Text property of the HyperLink... link.Text = "whatever"
Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx

Server.UrlEncode apostrophe(') in Firefox

So I have a Hyperlink called lnkTwitter:
And I'm trying to set the url in the code behind:
lnkTwitter.NavigateUrl = string.Format("http://www.twitter.com/home?status={0}", Server.UrlEncode("I'm Steven"));
When I do that and hover over the link, the url displays correctly in the status bar as "http://www.twitter.com/home?status=I'm+Steven", but the actual url, if I click on the link or look at the link's properties, is "http://www.twitter.com/home?status=I%27m+Steven".
For some reason, this only happens in Firefox; in IE, I am taken to the correct url.
Have you tried to view source code?
If source is ok, then there's no troubles with your code.
Firefox just likes to unescape the urls that it shows. While this can be confusing it should not cause your code or the sites you link to (twitter, in this case) any problems.
If you follow the link and then copy the url and paste it into Notepad or something then you should get the escaped form that was actually used instead of the unescaped form that was displayed.
Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode are available to use in C# out of the box, they can not convert all the characters exactly the same way as JavaScript escape function does.
Solution: Use JScript.Net's own implementation. Simply reference the Microsoft.JScript.dll and use the Microsoft.JScript.GlobalObject.escape() method to encode your url.

Categories