What's the easiest way to change the contents of text in a string with C#?

Posted by Anne on Stack Overflow See other posts from Stack Overflow or by Anne
Published on 2012-12-20T10:00:06Z Indexed on 2012/12/20 11:03 UTC
Read the original article Hit count: 160

Filed under:

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#?

© Stack Overflow or respective owner

Related posts about c#