I'm trying to figure out the best way to get everything before the / character in a string. Some example strings are below.
var url = dr.FindElements(By.XPath("//*[#id=\"u_0_3\"]/div/h1/a"));
foreach (var item in url)
{
if (item.GetAttribute("href").ToString().Contains("https://www.facebook.com/"))
{
listBox4.Items.Add("here");
}
}
the href is like that = "http://facebook.com/xxx"
want the xxx which is username want to get it alone in my listbox without the rest of the url
If you're at the point where you've got the string you want to work with, here are two ways to do this:
Split the string by / and take the last part
var stringToProcess = "https://www.facebook.com/ProfileName";
var partsOfString = stringToProcess.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var profileName = partsOfString.Last();
Use the Uri class to extract the last part
var stringToProcess = "https://www.facebook.com/ProfileName";
var stringToProcessAsUri = new Uri(stringToProcess);
var profileNameFromUri = stringToProcessAsUri.Segments.Last();
This is the "strictly better" way as it will give you a clean result even if the profile address has a query string attached to it, i.e:
var stringToProcess = "https://www.facebook.com/ProfileName?abc=def";
var stringToProcessAsUri = new Uri(stringToProcess);
var profileNameFromUri = stringToProcessAsUri.Segments.Last();
You'll still have the variable profileNameFromUri returned containing only ProfileName
I have the following JSON in a text file which I am trying to parse.
{
"0":[68],
"1":[154,78,61],
"2":[89,132,146],
"3":[],
"4":[77,132,146],
"5":[32,132,50],
"6":[],
"7":[114,118,54,44,72,136,156,134,129,82,43,34,51,93,142,67,47,153,160,73,39,149,107,94,145,29,115,53,83,1,35,56,123,66,90,121,155],
"8":[89,146],
"9":[89,146],
"10":[100,135],
"11":[],
"12":[],
"13",[111,131],
"14":[77,124],
"15":[89,146],
"16":[163,126,122],
"17":[100,126,135],
"18":[32,50],
"19":[163,126,122]
}
The code I have is
var map = new List<Dictionary<int, List<int>>>();
using (var r = new StreamReader(#"C:\Development\phase2\dependencymap.json"))
{
var json = r.ReadToEnd();
map = JsonConvert.DeserializeObject<List<Dictionary<int, List<int>>>>(json);
}
But it doesn't seem to like the format. What am I doing wrong?
The JSON is malformed. Check the following line
"13" , [111,131],
and change it to:
"13" : [111,131],
Try map = JsonConvert.DeserializeObject<List<Dictionary<String, List<int>>>>(json);
Your keys are String, not int.
Currently what I'm doing is serializing the JsonResult.Data then Deserialized into dynamic variable before looping in each row and get the Document. Is there any way to handle this? Thanks
if (string.IsNullOrWhiteSpace(searchTerm_))
searchTerm_ = "*";
_azureSearch = new AzureSearchService("afindexdev");
JsonResult result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = _azureSearch.SearchAssetFactory(searchTerm_).Results
};
string json = JsonConvert.SerializeObject(result.Data);
var resultJsonString = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
foreach (dynamic row in resultJsonString)
{
var associatemItem = new AssociatedItem();
associatemItem.Id = row.Document.Id;
associatemItem.Title = row.Document.Title;
associatemItem.Type = row.Document.Type;
searcResult.AssociatedItems.Add(associatemItem);
}
What about this?
var associatedItem = Newtonsoft.Json.JsonConvert.DeserializeObject< List < AssociatedItem > >(json);
That way you don't have to make your object yourself.
You can define your model with properties which you want to deserialize with attribute [SerializePropertyNamesAsCamelCase]. This attribute is included to Microsoft.Azure.Search library. After that, all you need to do is to define your model in search generic - like this Hotel class
var sp = new SearchParameters();
if (!String.IsNullOrEmpty(filter))
{
sp.Filter = filter;
}
DocumentSearchResult<Hotel> response = indexClient.Documents.Search<Hotel>(searchText, sp);
You can find more info here
I'm trying to render the FileContentResult's (.png files in my case) into a base64 strings to return them into json.
I've found an interesting approach from here: http://approache.com/blog/render-any-aspnet-mvc-actionresult-to/ , that supposedly doing what i need, but when I'm trying to do something like
public async ActionResult GetUsers()
{
...
var query = from user in otherUsers
join file in allFiles on user.Profile.Id equals file.Profile.Id into usersWithFiles
from userWithFile in usersWithFiles.DefaultIfEmpty(new File(){Content = new byte[0], ContentType = "image/png"})
select new UserFriendModel { Id = user.Id, UserName = user.UserName, ProfileId = user.Profile.Id, File = File(userWithFile.Content, userWithFile.ContentType).Capture(ControllerContext) };
return Json(query.ToList(), JsonRequestBehavior.AllowGet);
}
I'm getting
System.Web.HttpException:"OutputStream is not available when a custom
TextWriter is used" thrown at result.ExecuteResult(controllerContext);
line.
You can get the file (png image) in a separate query, transform the data into text format by using your text writer, and then inject it into your data model.
Some psudo code like this:
var file = GetFileQuery();
var fileString = TextWriter.Convert(file);
var query = from user in otherUsers
join file in allFiles on user.Profile.Id equals file.Profile.Id into usersWithFiles
from userWithFile in usersWithFiles.DefaultIfEmpty(new File(){Content = new byte[0], ContentType = "image/png"})
select new UserFriendModel { Id = user.Id, UserName = user.UserName, ProfileId = user.Profile.Id, File = fileString };
Another way to do it is to use 2 different action to handle the request. First action returns all the normal data for your data model, and 2nd action only returns you the image. This way you have more flexibility with your image format, i.e. you can return it in OutputStream as PNG image (without explicit serialization/deserialization).
That's my 2 cents.
Henry
I have one HTML table. In the table I added a tr during run-time using the javascript code below -
var table = document.getElementById("myTable");
var tab_length= table.rows.length;
var row = table.insertRow(tab_length);
row.id="Row"+tab_length;
var Ord_ID = row.insertCell(0);
var Ord_Name = row.insertCell(1);
var Ord_Qty = row.insertCell(2);
var Ord_Price = row.insertCell(3);
var Ord_Total = row.insertCell(4);
var Del_Button = row.insertCell(5);
var Edit_Button = row.insertCell(6);
Ord_ID.innerHTML = document.getElementById("Ord_ID").value;
Ord_Name.innerHTML = document.getElementById("Ord_Name").value;
Ord_Qty.innerHTML = document.getElementById("Ord_Qty").value;
Ord_Price.innerHTML = document.getElementById("Ord_Price").value;
But I can not access the tr values in c#. Can anyone please help with this problem?
I would use JQuery to send an Ajax POST request. If you want to do error handling, you can return either a success or error message in JSON format. Have a look at this link for more information:
http://api.jquery.com/jquery.ajax/