What's the easiest way to change the contents of text in a string with C#?
- by Anne
I have HTML in a string that looks like this:
<div id="control">
<a href="/xx/x">y</a>
<ul>
<li><a href="/C003Q/x" class="dw">x</a></li>
<li><a href="/C003R/xx" class="dw">xx</a></li>
<li><a href="/C003S/xxx" class="dw">xxx</a></li>
</ul>
</div>
I would like to change this to the following:
<div id="control">
<a data-href="/xx/x" ><span>y</span></a>
<ul>
<li><a data-href="/C003Q/x" class="dw"><span>x</span></a></li>
<li><a data-href="/C003R/xx" class="dw"><span>xx</span></a></li>
<li><a data-href="/C003S/xxx" class="dw"><span>xxx</span></a></li>
</ul>
</div>
I heard about regex but I am not sure how I can use it to change something inside the address tags and to change href at the same time. Would I need to use regex twice and can I change the inside of the <a ... >...</a> using regex or is there an easier way with C#?