I'm trying to figure out how I can get the last text inside node which is the ID 104543.
<id>tag:website.com:feed/web/main/104543</id>
The output should be 104543.
If you are using .NET 3.5 or higher, then you could use the XElement class in System.Xml.Linq.
You could retrieve the tag element content in the following way:
string str = #"<id>tag:website.com:feed/web/main/104543</id>";
XElement element = XElement.Parse(str);
var content = element.Descendants("id").FirstOrDefault().Value;
Now, parsing the content depends on how this is structured: if the code you want to extract will always be placed after the last "/" character, then you could do the following:
string code = content.Split(new[] { "/" }, StringSplitOptions.None).Last();
Related
I have textbox1 field in asp.net and a text area to show count of records.
I want to count the records split by , in textbox1 but when textbox1 is empty text area is showing 1.
Here is the code.
int contacts = textbox1.Text.Split(',').Count();
textarea.Text = contacts.ToString();
String.Split always returns at least one string, if you pass string.Empty you will get one string which is the input string(so in this case string.Empty).
Documentation:
....
If this instance does not contain any of the characters in separator,
the returned array consists of a single element that contains this instance.
You have to check it, f.e. with string.IsNullOrEmpty(or String.IsNullOrWhiteSpace):
int contacts = 0;
if(!string.IsNullOrEmpty(textbox1.Text))
contacts = textbox1.Text.Split(',').Length;
Try this
int contacts = string.IsNullOrEmpty(string.textbox1.Text)? string.empty: textbox1.Text.Split(',').Count();
textarea.Text = contacts.ToString();
This is because even when textbox1.Text is an empty string, that's still treated as one item. You need to use StringSplitOptions.RemoveEmptyEntries so that empty entries are ignored when producing the result of calling Split:
var contacts = textbox1.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Count();
To decompose what you've written into individual statements, what you have is:
var items = textbox1.Text.Split(new char[] { ', ' }, StringSplitOptions.RemoveEmptyEntries);
var countOfItems = itemsFromText.Count();
If you look at items you'll see that it's an array of strings (string[]) which contains one entry for each item in the text from textbox1.Text.
Even if an empty string is passed in (i.e. textbox1 is empty) there's still one string to be returned, hence the fact that your code as written is returning 1, whereas in countOfItems where I've broken the code apart it will have 0 because of the use of StringSplitOptions.RemoveEmptyEntries.
The documentation on msdn of the String.Split overload that takes StringSplitOptions as a parameter has more examples and detail about this.
I am generating a link using below code
string EncryptPath = Common.Encrypt(Path);
string SourceLinkPath= string.Empty;
if (File.Exists(Server.MapPath("Image.txt")))
{
SourceLinkPath = System.IO.File.ReadAllText(Server.MapPath ("Image.txt"));
}
string link2 = SourceLinkPath + EncryptPath;
TxtPathLink2.Text = link2;
the link is generating but it is giving space after sourcepath. OUTPUT like
http://18.10.10.11/test/View.aspx?Value=
67534ERT
i want to generate like http://18.10.10.11/test/View.aspx?Value=67534ERT
How can i generate link in one line
The .txt file probably has a whitespace you are missing.
Change System.IO.File.ReadAllText(Server.MapPath ("Image.txt"))
To:
System.IO.File.ReadAllText(Server.MapPath("Image.txt")).Trim()
String.Trim() removes all leading and trailing white-space characters from the String object.
XML
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-04-16T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
<CalendarFairs>
Code
var elements = from element in doc.Descendants("CalendarFair")
where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now
select new
{
dateStart = element.Element("DateStart").Value,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
foreach (var item in elements)//send this error
{}
System.FormatException: The string was not recognized as a valid DateTime. There is a
unknown word starting at index 0.
why error?
Try to change it as follows:
var elements = from element in doc.Descendants("CalendarFair")
let start = element.Element("DateStart").Value
where DateTime.Parse (start)==DateTime.Now.Date
select new
{
dateStart = start,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
EDIT: based on the XML you have posted the query above works pretty well. Try to test it with this input:
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-03-20T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
</CalendarFairs>
Note that I have inserted today's start date. Actually I think the result was empty just because there weren't entries with actual date.
It sounds like one or more of your input <DateStart> strings is not in a valid DateTime format.
Can you post some sample input XML?
It may be that you need to provide the date format using ParseExact - http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
I have a quick question about JQuery. I have dynamically generated paragraphs with id's that are incremented. I would like to take information from that page and bring it to my main page. Unfortunately I am unable to read the dynamically generated paragraph IDs to get the values. I am trying this:
var Name = ((data).find("#Name" + id).text());
The ASP.NET code goes like this:
Dim intI As Integer = 0
For Each Item As cItem in alProducts1
Dim pName As New System.Web.UI.HtmlControls.HtmlGenericControl("p")
pName.id = "Name" & intI.toString() pName.InnerText = Item.Name controls.Add(pName) intI += 1
Next
Those name values are the values I want...Name1, name2, name3 and I want to get them individually to put in their own textbox... I'm taking the values from the ASP.NET webpage and putting them into an AJAX page.
Your question is not clear about your exact requirement but you can get the IDs of elements with attr method of jQuery, here is an example:
alert($('selector').attr('id'));
You want to select all the elements with the incrementing ids, right?
// this will select all the elements
// which id starts with 'Name'
(data).find("[id^=Name]")
Thanks for the help everyone. I found the solution today however:
var Name = ($(data).find('#Name' + id.toString()).text());
I forgot the .toString() part and that seems to have made the difference.
I need to get the value of the item clicked and the name of the columns.
for each(item in colunas) {
var itemok:String = item.dataField;
Alert.show(''+datagridlist.selectedItem.itemok); // show value of column
}
But this way it returns 'undefined'.
But if I put the name already in function, I can get the correct data, example:
Alert.show(''+datagridlist.selectedItem.create); // create is a column name in mysql
But this variable must be created dynamically, example:
var itemok:String = item.dataField;
Alert.show(''+datagridlist.selectedItem.itemok); // show value of column
Could someone help me? I'm at it on time and I can not convert the string to column name.
I thank you all now
for each(item in colunas)
{
var itemok:String = item.dataField;
Alert.show(''+datagridlist.selectedItem[itemok]);
}
The dot syntax to access properties/fields works only with property names. When the property name is stored in a string, use square brackets.
var t:String = "value";
//The following three lines are the same and will work
trace(something.value);
trace(something["value"]);
trace(something[t]);
//but this one won't
trace(something.t);