謝謝大家。經過幾乎整天的思考和編程之後,我決定必須使用native htmlElement而不是htmlagilitypack HtmlNode,因爲我想在webbrowser的Htmlelement中輸入文本。所以這裏是我提出的代碼。如果有人用htmlagilitypack顯示解決方案,我仍然很感激。
public HtmlElement selectHtmlNode(string xPath, HtmlElement htmlElement)
{
string currentNode;
int indexOfElement;
//get string representation of current Tag.
if (xPath.Substring(1,xPath.Length-2).Contains('/'))
currentNode = xPath.Substring(1, xPath.IndexOf('/', 1) - 1);
else
currentNode = xPath.Substring(1, xPath.Length-1);
//gets the depth of current xPath
int numOfOccurence = Regex.Matches(xPath, "/").Count;
//gets the children's index
int.TryParse(Regex.Match(currentNode, @"\d+").Value, out indexOfElement);
//if i have to select nth-child ex: /tr[4]
if (indexOfElement > 1)
{
currentNode = currentNode.Substring(0, xPath.IndexOf('[') - 1);
//the tag that i want to get
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.Children[indexOfElement - 1];
}
//still has some children tags
if (numOfOccurence > 1)
{
int i = 1;
//select nth-child
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode && i == indexOfElement)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
else if (tempElement.TagName.ToLower() == currentNode && i < indexOfElement)
{
i++;
}
}
}
}
else
{
if (numOfOccurence == 1 || numOfOccurence == 0)
{
return htmlElement.FirstChild;
}
if (numOfOccurence > 1)
{
foreach (HtmlElement tempElement in htmlElement.Children)
{
if (tempElement.TagName.ToLower() == currentNode)
{
return selectHtmlNode(xPath.Substring(xPath.IndexOf('/', 1)), tempElement);
}
}
}
}
return null;
}
函數以這種方式被調用。其中htmlController是某個類的實例。
HtmlElement currentElement = htmlController.selectHtmlNode("/body/form/div/div[2]/div/div/input", webBrowser.Document.GetElementsByTagName("html")[0]);
currentElement.SetAttribute("Value", "hello world");
通話的ToString() –
@MaziarBouali嗨,我如何可以選擇轉換Htmlnode串後具體HTML元素? –