Often working with content in the form of html, I have needed to manipulate the content intelligently. I accomplished this by using regular expressions to "parse" the html to find certain tags. This enabled me to look for certain tags with certain attributes, etc.
This works well enough, but some people aren't familiar with regular expression syntax and struggle to maintain and extend the code for manipulating the markup.
A much simpler and developer-friendly option is to reference the mshtml object. I will illustrate the use of this object with an over simplified example. I am going to mention regular expressions, but I'm not going to go into the syntax or even show any statements - it's a totally different subject altogether.
-----------------------------------------
My pages in my website contains elements with formatting elements hard coded onto them, instead of having all the formatting set through a class reference to a stylesheet.
This means that I will have an element with it's bgcolor attribute set to "blue" and it's border attribute set to "1". For example:
<p bgcolor="blue" color="red" border="1">bla di bla bla</p>
I want to set a class name attribute on all the elements, with a combination of these two attributes with the same values. Meaning any element having a bgcolor of "blue" and a border of "1". The following will qualify too:
<td bgcolor=blue id="mytd" onclick="alert('clicked');" border="1">Hello</td>
So how can I find all the instances of tags that have these two attributes with the correct values in the markup? A normal string operation will not suffice. So a regular expression solution is sufficient. But when the border and bgcolor sequence is switched it adds a whole new level of complexity to the regular expression, for example:
<td border="1" id="mytd" onclick="alert('clicked');" bgcolor=blue>Hello</td>
Now we can't assume that the bgcolor attribute will be found first and then the border attribute. And what about when we want to search on three attributes?
Now we can't assume that the bgcolor attribute will be found first and then the border attribute. And what about when we want to search on three attributes?
What we want to do is loop through the html elements in the markup and look for elements that satisfy our requirements, and we check this by accessing the attributes in a non-sequential, natural manner. If all the attributes are satisfied, then the tag qualifies for the update.
We need a way to let our method know what attributes to look for, their corresponding values and the new attribute key/value pairs to set ons this object.
Code
We have to add a reference to the mshtml object
In the solution explorer, highlight the project to which you want to add the parsing functionality
In the menu, click on Project -> Add reference
In the dialog box that is shown, under the .Net tab - choose the Microsoft.mshtml assembly
Click the select button and click on the OK button
Now we can reference this assembly
using mshtml;
Our class will contain one method, this method will take 3 parameters.namespace
本文地址:http://com.8s8s.com/it/it41148.htm