I need to get some text from a website we are using to get our data from. I finally found how, using HtmlAgilityPack and finding the Xpath I'm able to print out some text from the website.
But when I try to print the date and kind, which is coded like this:
<span class="span-line-break">zaterdag 05 december 2020</span> //Date
<span class="afvaldescr">Papier en karton</span> //Kind
I can't reach these two strings using my current code:
public string Postalcode = "6093DK";
public string Number = "2";
public string Add = "";
string url = "https://mijnafvalwijzer.nl/nl/" + Postalcode + "/" + Number + "/" + Add;
var web = new HtmlAgilityPack.HtmlWeb();
HtmlDocument doc = web.Load(url);
string when = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]/div/table[1]/tbody/tr/td[1]/a/p/span[1]")[0].InnerText;
string what = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]/div/table[1]/tbody/tr/td[1]/a/p/span[2]")[0].InnerText;
textBox1.Text = when;
textBox2.Text = what;
I figured that because the text is in a class I can not reach it.
Can someone help me find a more specific route to these strings?
The website is a Dutch garbadge calendar, don't mind it.
Browser inserts tbody for table element although it is not present in html. So here I just removed tbody from your XPath. In Chrome you can use network tab for viewing original response
string when = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]/div/table[1]/tr/td[1]/a/p/span[1]")[0].InnerText;
string what = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]/div/table[1]/tr/td[1]/a/p/span[2]")[0].InnerText;
You can also use shortened version of XPath using "//" and class selectors
string when = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]//table[1]//span[#class=\"span-line-break\"]")[0].InnerText;
string what = doc.DocumentNode.SelectNodes("//*[#id=\"december-2020\"]//table[1]//span[#class=\"afvaldescr\"]")[0].InnerText;
Related
How to write xpath for the below HTML:
<span id="filename_548948">Test DC Email </span>
The following xpath doesn't seem to work:
Driver.FindElement(By.XPath(".//span[text() = '" + nameOfEmail + "']")).Click();
The solution depends on what the string nameOfEmail contains.
You have an xpath query on exact text. Meaning every character should be the same in the search as on the webpage.
So if string nameOfEmail = "Test DC Email "
It will search properly.
Also, losing the . in front of the // might help
As per the HTML you have shared you can use the following xpath :
//with a constant string
Driver.FindElement(By.XPath("//span[starts-with(#id,'filename_') and contains(normalize-space(), 'Test DC Email')]")).Click();
//with a variable string
Driver.FindElement(By.XPath("//span[starts-with(#id,'filename_') and contains(normalize-space(), '" + nameOfEmail + "')]")).Click();
I have an XML which is passes as a string variable to me. I want to get the value of specific tags from that XML. Following is the XML I have and what I'm trying to achieve:
<code>
string xmlData = #"
<HEADER>
<TYPE>AAA</TYPE>
<SUBTYPE>ANNUAL</SUBTYPE>
<TYPEID>12345</TYPEID>
<SUBTYPEID>56789</SUBTYPEID>
<ACTIVITY>C</ACTION>
</HEADER>";
var typeId = data.Split("<TYPEID>")[0]; //Requirement
var activity = data.Split("<ACTIVITY>")[0]; //Requirement
</code>
I know string.Split(); doesn't work here as it requires a single character only. Other alternate is to use regex which seems a bit threatening to me. Although I have tried to work with it but doesn't getting the desired result. Can someone help with the regex code?
You should have used XML Parsing to get the values but since you are trying split to split a string from a string and not char you can choose
string typeId = xmlData.Split(new string[] { "<TYPEID>" }, StringSplitOptions.None)[1];
string typeIdVal = typeId.Split(new string[] { "</TYPEID>" }, StringSplitOptions.None)[0];
and it looks very neat and clean with XML Parsing
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load("yourXMLFile.xml");
XmlNodeList XTypeID = xmlDoc.GetElementsByTagName("TYPEID");
string TypeID = XTypeID[0].InnerText;
You can also choose SubString like
string typeidsubstr = xmlData.Substring(xmlData.IndexOf("<TYPEID>") + 8, xmlData.IndexOf("</TYPEID>") - (xmlData.IndexOf("<TYPEID>") + 8));
I used +8 because the length of <TYPEID> is 8 you can also choose it string.length to evaluate the result.
You can use XML Linq objects to parse these.
NB: There is a typo in the ACTIVITY element, the closing tag should be /ACTIVITY, not /ACTION! (I've corrected below)
string xmlData = #"<HEADER>
<TYPE>AAA</TYPE>
<SUBTYPE>ANNUAL</SUBTYPE>
<TYPEID>12345</TYPEID>
<SUBTYPEID>56789</SUBTYPEID>
<ACTIVITY>C</ACTIVITY>
</HEADER>";
var doc = XDocument.Parse(xmlData);
var typeId = doc.Root.Elements("TYPEID").First().Value;
var activity = doc.Root.Elements("ACTIVITY").First().Value;
I have a textbox where users can paste a URL address. I want to add a directory name to the URL before saving it in the database.
<asp:TextBox ID="urlTextbox" runat="server"></asp:TextBox>
Code behind
TextBox url = urlTextbox as TextBox;
string urlString = urlTextbox.Text;
Let's say the urlString = "mydomain.com/123456". I want to replace it with "mydomain.com/directory/123456". mydomain.com/directory is the same for all the URLs. The last part "123456" changes only.
Thank you
I'd suggest seeing if your needs are met with the UriBuilder class.
UriBuilder url = new UriBuilder(urlTextbox.Text);
Now you can use the various properties to change your url.
string formattedUrl = string.Format("{0}://{1}/directory/{2}", url.Scheme, url.Host, url.Path);
A better idea is to adjust the URL with another / same UriBuilder as noted by Jared.
UriBuilder url = new UriBuilder(urlTextbox.Text);
url.Path = string.Format("directory/{0}", url.Path);
Use this object as a Uri by simply doing this
Uri formattedUrl = url.Uri;
Or convert to a string if needed.
string formattedUrl = url.ToString();
You can also use Uri.TryParse(...) to verify if it's a valid URL being entered into the text box.
To get the individual query parameters, you can look at the Uri object.
UriBuilder url = new UriBuilder("mydomain.com/123456?qs=aaa&bg=bbb&pg=ccc");
url.Path = string.Format("directory/{0}", url.Path);
Uri formattedUrl = url.Uri;
string queryString = formattedUrl.Query;
// parse the query into a dictionary
var parameters = HttpUtility.ParseQueryString(queryString);
// get your parameters
string qs = parameters.Get("qs");
string bg = parameters.Get("bg");
string pg = parameters.Get("pg");
You can use string functions Split and Join to achieve your result. An example code is shown below
List<string> parts = urlString.Split(new char[] { '/'}).ToList();
parts.Insert(parts.Count - 1, "directory");
urlString = string.Join("/", parts);
This is one way of doing. Split the urlString using .split() function.
string[] parts = urlString.Split('/');
parts[parts.Length-1] will have that number. Append it to the string you want.
I'd do something like this:
//Assuming the address in urlString has the format mydomain.com/123456
string[] urlParts = urlString.Split('/');
string directory = "directory";
string finalUrl = urlParts[0] + "/" + directory + "/" + urlParts[1];
Be careful if the address has other "/" characters, like if preceded by http:// or something like that.
Hope it helps.
Simply use concatenation:
save in a temporary string
temp="mydomain.com/directory/"
and save the changing part in another string like
temp2="123456"
now concatenate both temp1 and temp2 like below.
urlString=temp1+temp2;
This might be a problem with Session and not ToString(), I'm not sure.
I have two .aspx pages and I want to pass an IP address from a datatable from one page to the other. When I do this, spaces get added that I don't want. The simple version of the code is this:
first .aspx page
int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");
test.aspx page
string ipCamAdd = Session["camera"].ToString();
TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";
what I want to print is
http ://ipadd/jpg/image.jpg?resolution=320x240
but what prints out is
http//ipaddress /jpg/image.jpg?resolution=320x240
how can I fix this?
Also, I asked this question hoping someone could tell me why this is happening as well. Sorry for the mistake.
Try this:
string ipCamAdd = Session["camera"].Trim().ToString();
For the valid concern, Session["camera"] could be null, add function such as the following to your code
static string ToSafeString(string theVal)
{
string theAns;
theAns = (theVal==null ? "" : theVal);
return theAns;
}
Then use:
string ipCamAdd = Session["camera"].ToSafeString().Trim();
You can use string.Replace if you just want to get rid of the spaces:
TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
Trim the result before setting to session.
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();
Seems In SQL your data type is char(*) if you convert the data type to varchar and re enter data, you wont get any additional spaces
I have created the yahoo weather API app using ASP.Net MVC 3 and when I tried to insert the postcode to the text field to find the correct xml, I wanted to leave gap for the standard UK postcode. Can you please help me to do that. The following code's model.PostCode represents the PostCode variable which has declared as string in model. This code is in the controller file.
private Boolean LookupWeather(ref RssModels model)
{
string WoeidUrl = "http://where.yahooapis.com/v1/places.q('" +
model.PostCode +
"')?appid=EzZDnOXV34EzJpQ8mX8mc62cYk1Gu21DzUhsLr.4nQ2qz.xffZah.RNq8lObxA--";
XDocument getWoeid = XDocument.Load(WoeidUrl);
try
{
model.Woied = (int)(from place in getWoeid.Descendants("place")
select place.Element("woeid")).FirstOrDefault();
return true;
}
catch
{
return false;
}
If you can please help me to get the URL like follows.
http://where.yahooapis.com/v1/places.q('mk10%202hn')?appid=EzZDnOXV34EzJpQ8mX8mc62cYk1Gu21DzUhsLr.4nQ2qz.xffZah.RNq8lObxA--
Thank you in advance.
Use UrlEncode
string WoeidUrl = "http://where.yahooapis.com/v1/places.q('"
+ UrlEncode(postCode)
+ "')?appid=EzZDnOXV34EzJpQ8mX8mc62cYk1Gu21DzUhsLr.4nQ2qz.xffZah.RNq8lObxA--";
All the browser is doing for that is replacing the space with %20, you can replicate this just by using Replace, try:
string postCode = model.PostCode.Replace(" ", "%20");
Then use it in your code above like so:
string WoeidUrl = "http://where.yahooapis.com/v1/places.q('"
+ postCode
+ "')?appid=EzZDnOXV34EzJpQ8mX8mc62cYk1Gu21DzUhsLr.4nQ2qz.xffZah.RNq8lObxA--";