更换使用HTML敏捷性包一个HTML DIV标签的InnerText [英] Replacing a HTML div InnerText tag using HTML Agility Pack
问题描述
我使用的HTML敏捷性包处理和编辑HTML文档。我想改变在该领域的文本,如这样的:
I'm using the HTML Agility Pack to manipulate and edit a HTML document. I want to change the text in the field such as this:
<div id="Div1"><b>Some text here.</b><br></div>
我期待在这个div是更新文本:
I am looking to update the text within this div to be:
<div id="Div1"><b>Some other text.</b><br></div>
我试着这样做使用以下code,但它似乎并不奏效,因为InnerText属性是只读的。
I've tried doing this using the following code, but it doesn't seem to be working because the InnerText property is readonly.
HtmlTextNode hNode = null;
hNode = hDoc.DocumentNode.SelectSingleNode("//div[@id='Div1']") as HtmlTextNode;
hNode.InnerText = "Some other text.";
hDoc.Save("C:\FileName.html");
我在做什么错在这里?正如上面提到的,是的InnerText一个只读字段,尽管它写它获取或设置的文件中。有没有通过它可以做到这一点的另一种方法?
What am I doing wrong here? As mentioned above, the InnerText is a read only field, although it's written in the documentation that it "gets or sets". Is there an alternate method through which this can be done?
推荐答案
这位前pression用在这里: // DIV [@ ID ='Div1构成']
选择了 DIV
,这是不是一个 HtmlTextNode
,所以 HNODE
变量包含空
在你的例子。
The expression is used here: //div[@id='Div1']
selects the div
, which is not a HtmlTextNode
, so the hNode
variable holds null
in your example.
的的InnerText
属性是真的只读的,但 HtmlTextNode
拥有财产文本
其可用于设置必要的值。但是在这之前,你应该得到的文本节点。这可能与此前pression很容易做到: // DIV [@ ID ='Div1构成'] // B //文本()
:
The InnerText
property is realy read-only, but HtmlTextNode
has property Text
which could be used to set the necessary value. But before this you should get that text node. This could be easily done with this expression: //div[@id='Div1']//b//text()
:
hNode = hDoc.DocumentNode
.SelectSingleNode("//div[@id='Div1']//b//text()") as HtmlTextNode;
hNode.Text = "Some other text.";
这篇关于更换使用HTML敏捷性包一个HTML DIV标签的InnerText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!