C#获取HTML对应标签 <div><div><p></p></div><div><p></p></div></div>如何获取HMTL对应的标签 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var oDiv=document.getElementsByTagName('div');alert(oDiv.length)var oDiv1=document.getElementsByTagName('p');alert(oDiv1.length) <div> <div> <p> </p> </div> <div> <p> </p> </div> </div> 这里有多个DIV啊,好像这样不可以吧也不想用JS实现,做winform程序 <div class=class> 方法<div> <p> abc</p> </div> <div> <p> def</p> </div> 呃呃呃</div> 比如想要上面中的方法<div> <p> abc</p> </div> <div> <p> def</p> </div> 呃呃呃 Match m = Regex.Match(content, "<(/?)(\w+).*?>", RegexOptions.IgnoreCase | RegexOptions.Multiline);string aa = m.Groups[1].Value; 这么写么,不太明白 <div class=class> 方法 <div> <p> abc </p> </div> <div> <p> def </p> </div> 呃呃呃 </div> 。。上下都有内容,想获取class=class 的DIV标签中的内容 C# code: Page.ClientScript.ReGisterClientScript(typeof(string),"js1","<script>var oDiv=document.getElementsByTagName('div');var oDiv1=document.getElementsByTagName('p');</script>"); LZ 再自己扩充下 给你2个实力:方法一:使用TextRange(IHTMLTxtRange) 有了上一个Case的基础,相信大家立刻会想到使用TextRange。没错,TextRange除了提供查找方法之外,还提供了一个pasteHTML方法,以指定的HTML文本替换当前TextRange中的内容。代码片断如下: public partial class HilightDemo : Form { // 定义高亮显示效果的标签。 string tagBefore = "<span style='background-color:yellow;color:black'>"; string tagAfter = "</span>"; // …… private void btnHilight_Click(object sender, EventArgs e) { HtmlDocument htmlDoc = webBrowser.Document; string keyword = txtKeyword.Text.Trim(); if (keyword == "") return; object oTextRange = htmlDoc.Body.InvokeMember("createTextRange"); mshtml.IHTMLTxtRange txtrange = oTextRange as mshtml.IHTMLTxtRange; while (txtrange.findText(keyword, 1, 4)) { try { txtrange.pasteHTML(tagBefore + keyword + tagAfter); } catch { } txtrange.collapse(false); } } }※这段代码里获取IHTMLTxtRange的方式和上面的例子稍稍不同,其实所谓条条大路通罗马,本质是一样的。 方法二:使用DOM(文档对象模型) 将HTML文档解析为DOM,然后遍历每个节点,在其中搜索关键字并进行相应替换处理即可。 public partial class HilightDemo : Form { //…… private void btnHilight_Click(object sender, EventArgs e) { HTMLDocument document = (HTMLDocument)webBrowser.Document.DomDocument; IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser.Document.Body.DomElement; string keyword = txtKeyword.Text.Trim(); if (keyword == "") return; HilightText(document, bodyNode, keyword); } private void HilightText(HTMLDocument document, IHTMLDOMNode node, string keyword) { // nodeType = 3:text节点 if (node.nodeType == 3) { string nodeText = node.nodeValue.ToString(); // 如果找到了关键字 if (nodeText.Contains(keyword)) { IHTMLDOMNode parentNode = node.parentNode; // 将关键字作为分隔符,将文本分离,并逐个添加到原text节点的父节点 string[] result = nodeText.Split(new string[] { keyword }, StringSplitOptions.None); for (int i = 0; i < result.Length - 1; i++) { if (result[i] != "") { IHTMLDOMNode txtNode = document.createTextNode(result[i]); parentNode.insertBefore(txtNode, node); } IHTMLDOMNode orgNode = document.createTextNode(keyword); IHTMLDOMNode hilightedNode = (IHTMLDOMNode)document.createElement("SPAN"); IHTMLStyle style = ((IHTMLElement)hilightedNode).style; style.color = "black"; style.backgroundColor = "yellow"; hilightedNode.appendChild(orgNode); parentNode.insertBefore(hilightedNode, node); } if (result[result.Length - 1] != "") { IHTMLDOMNode postNode = document.createTextNode(result[result.Length - 1]); parentNode.insertBefore(postNode, node); } parentNode.removeChild(node); } // End of nodeText.Contains(keyword) } else { // 如果不是text节点,则递归搜索其子节点 IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection; foreach (IHTMLDOMNode n in childNodes) { HilightText(document, n, keyword); } } } } 另外, 你可以用WebClient类的DownLoad方法. 获取到请求的字符串.. 然后用string.subString 慢慢找... 不知道上面这几个是否跑题拉? C#解析网页数据出错。未将对象引用设置到对象的实例 字段中包含括号,该怎么写才能不出错? 求助,此代码运行后就卡死,‘没有反应’ WPF 动态添加控件,并实现透明渐进效果 关于数字水印的代码找错 c# api hook 全盘获取键盘的值 在C# FORM背景图片拉伸 如何在Winform 中,实现打印,打印预览 C#传输文件夹中的文件到服务器 出问题了 麻烦高手救命~~~在线等 窗体编程经验丰富者请进:窗体刷新速度的问题! 我想学学bindingsource是怎么回事,以下代码如何改 帮我修该一下下面的代码
alert(oDiv.length)var oDiv1=document.getElementsByTagName('p');
alert(oDiv1.length)
<div> <p> </p> </div> </div> 这里有多个DIV啊,好像这样不可以吧也不想用JS实现,做winform程序
<div class=class>
方法
<div> <p> abc</p> </div>
<div> <p> def</p> </div>
呃呃呃
</div>
比如想要上面中的
方法
<div> <p> abc</p> </div>
<div> <p> def</p> </div>
呃呃呃
string aa = m.Groups[1].Value;
这么写么,不太明白
<div class=class>
方法
<div> <p> abc </p> </div>
<div> <p> def </p> </div>
呃呃呃
</div>
。。上下都有内容,想获取class=class 的DIV标签中的内容
var oDiv1=document.getElementsByTagName('p');
</script>");
LZ 再自己扩充下
有了上一个Case的基础,相信大家立刻会想到使用TextRange。没错,TextRange除了提供查找方法之外,还提供了一个pasteHTML方法,以指定的HTML文本替换当前TextRange中的内容。代码片断如下:
public partial class HilightDemo : Form
{
// 定义高亮显示效果的标签。
string tagBefore = "<span style='background-color:yellow;color:black'>";
string tagAfter = "</span>"; // …… private void btnHilight_Click(object sender, EventArgs e)
{
HtmlDocument htmlDoc = webBrowser.Document;
string keyword = txtKeyword.Text.Trim();
if (keyword == "")
return; object oTextRange = htmlDoc.Body.InvokeMember("createTextRange"); mshtml.IHTMLTxtRange txtrange = oTextRange as mshtml.IHTMLTxtRange; while (txtrange.findText(keyword, 1, 4))
{
try
{
txtrange.pasteHTML(tagBefore + keyword + tagAfter);
}
catch { }
txtrange.collapse(false);
}
}
}※这段代码里获取IHTMLTxtRange的方式和上面的例子稍稍不同,其实所谓条条大路通罗马,本质是一样的。 方法二:使用DOM(文档对象模型)
将HTML文档解析为DOM,然后遍历每个节点,在其中搜索关键字并进行相应替换处理即可。
public partial class HilightDemo : Form
{
//…… private void btnHilight_Click(object sender, EventArgs e)
{
HTMLDocument document = (HTMLDocument)webBrowser.Document.DomDocument;
IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser.Document.Body.DomElement;
string keyword = txtKeyword.Text.Trim();
if (keyword == "")
return; HilightText(document, bodyNode, keyword);
} private void HilightText(HTMLDocument document, IHTMLDOMNode node, string keyword)
{
// nodeType = 3:text节点
if (node.nodeType == 3)
{
string nodeText = node.nodeValue.ToString();
// 如果找到了关键字
if (nodeText.Contains(keyword))
{
IHTMLDOMNode parentNode = node.parentNode;
// 将关键字作为分隔符,将文本分离,并逐个添加到原text节点的父节点
string[] result = nodeText.Split(new string[] { keyword }, StringSplitOptions.None);
for (int i = 0; i < result.Length - 1; i++)
{
if (result[i] != "")
{
IHTMLDOMNode txtNode = document.createTextNode(result[i]);
parentNode.insertBefore(txtNode, node);
}
IHTMLDOMNode orgNode = document.createTextNode(keyword);
IHTMLDOMNode hilightedNode = (IHTMLDOMNode)document.createElement("SPAN");
IHTMLStyle style = ((IHTMLElement)hilightedNode).style;
style.color = "black";
style.backgroundColor = "yellow";
hilightedNode.appendChild(orgNode); parentNode.insertBefore(hilightedNode, node);
}
if (result[result.Length - 1] != "")
{
IHTMLDOMNode postNode = document.createTextNode(result[result.Length - 1]);
parentNode.insertBefore(postNode, node);
}
parentNode.removeChild(node);
} // End of nodeText.Contains(keyword)
}
else
{
// 如果不是text节点,则递归搜索其子节点
IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection;
foreach (IHTMLDOMNode n in childNodes)
{
HilightText(document, n, keyword);
}
}
}
}
然后用string.subString 慢慢找...
不知道上面这几个是否跑题拉?