从输入元素字符串数组中提取值

时间:2018-10-07 09:17:48

标签: c# asp.net asp.net-mvc

我有一个像这样从<td>的{​​{1}}中读取的字符串数组

datatable

我怎样才能在c#中仅从中获取值。

我尝试了"<input id=\"item_Job_ID\" name=\"item.Job_ID\" type=\"text\" value=\"5036\">" ,但该方法无效。我可以使用Split("\\")来提取值吗?

先谢谢您

2 个答案:

答案 0 :(得分:1)

使用HTML Agility Pack。

 HtmlDocument doc = new HtmlDocument();
 string htmlContent = "<input id=\"item_Job_ID\" name=\"item.Job_ID\" type=\"text\" value=\"5036\">";
 doc.LoadHtml(htmlContent);
 HtmlNode inputNode = doc.DocumentNode.FirstChild;
 string value = inputNode.GetAttributeValue("value", "0");

答案 1 :(得分:1)

我认为,这对您有用

 string inputstr = "< input id =\"item_Job_ID\" name=\"item.Job_ID\" type=\"text\" value=\"5036\">";
 var splitdataList = inputstr.Split(new string[] { "\"", "=", " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
 var value = splitdataList.Contains("value") ? splitdataList[splitdataList.IndexOf("value") + 1] : ""; // Return 5036