Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Get anchor links from XhtmlString

$
0
0

Hi,

Is there a recommended way to extract anchor tags from an XhtmlString. So for example, I have this body copy -

<h1>Page title</h1><h2><a id="sub-title-1"></a>Sub title 1</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus rutrum ornare lectus id porttitor. Proin varius augue ipsum, nec tincidunt arcu efficitur in</p><h2><a id="sub-title-2"></a>Sub title 2</h2><p>Proin varius augue ipsum, nec tincidunt arcu efficitur in</p><p><a href="/episerver/CMS/Content/who,,141614/?epieditmode=False">Page link</a></p>

And I can get the anchor name doing something like this but wonder if there's a better way to do this?

@{
    string Pattern = @"(<a id="")(.*)("">)";
    Regex rgx = new Regex(Pattern);
    foreach (Match match in rgx.Matches(Model.BodyCopy.ToHtmlString()))
    {<h1>
            @match.Groups[2].Value</h1>
    }
}

I notice that's it's possible to use .Fragments property on an XhtmlString but that only seems to give me a long continuous like so (split across two entries)

"<h1>Page title</h1>\n<h2><a id=\"sub-title-1\"></a>Sub title 1</h2>\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus rutrum ornare lectus id porttitor. Proin varius augue ipsum, nec tincidunt arcu efficitur in</p>\n<h2><a id=\"sub-title-2\"></a>Sub title 2</h2>\n<p>Proin varius augue ipsum, nec tincidunt arcu efficitur in</p>\n<p><a href=\""

And a UrlFragment.

Fairly new to Episerver and would appreciate some advice. Thanks


Viewing all articles
Browse latest Browse all 8020

Trending Articles