HTML渲染与检查元素功能?如何C# [英] HTML Render with inspect element functionality? HOW TO C#

查看:189
本文介绍了HTML渲染与检查元素功能?如何C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个HTML Render,显示一个HTML文档,不需要在线网页。然后当我点击一个HTML控件时,它只显示我点击的HTML。真正的意图是从根元素获得xpath到选定的TAG。

解决方案

我认为你必须使用 System.Windows.Forms.WebBrowser 控件来加载你的html文档。重写Form的OnLeftButton事件。然后调用 WebBrowser.Document.GetElementFromPoint 方法。所以这个方法将返回 HtmlElement 类型的对象。因此,您将获得html元素,您可以从中导航到内部html源代码或通过选定标记中标记的层次结构进行导航;) 我创建了一些示例为你:

  private static String GetTagNameByClick(WebBrowser refWebBrowser,Int32 valScreenX,Int32 valScreenY)
{
Point refPoint = refWebBrowser.PointToClient(new Point(valScreenX,valScreenY));

HtmlElement refHtmlElement = refWebBrowser.Document.GetElementFromPoint(refPoint);

返回refHtmlElement.TagName;
}

祝你好运!


I want to do a HTML Render that shows a HTML Document, not necessary an online webpage. Then when I click over a HTML Control, it shows only the HTML where I clicked. The real intention is to get the xpath from the root element to the selected TAG.

解决方案

I think that you must use System.Windows.Forms.WebBrowser control for loading your html document. Override for example OnLeftButton event of the Form. And then call WebBrowser.Document.GetElementFromPoint method. So this method will return object of HtmlElement type. As the result you'll get html element from which you could navigate to inner html source code or navigate by hierarchy of tags from your selected tag;)

I create some example for you:

private static String GetTagNameByClick(WebBrowser refWebBrowser, Int32 valScreenX, Int32 valScreenY)
    {
        Point refPoint = refWebBrowser.PointToClient(new Point(valScreenX, valScreenY));

        HtmlElement refHtmlElement = refWebBrowser.Document.GetElementFromPoint(refPoint);

        return refHtmlElement.TagName;
    }

Good luck!

这篇关于HTML渲染与检查元素功能?如何C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆