Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

... I try to explain it in another way. I have a string like this:

string myText = "... <p class="MsoNormal">bla gezeichnete bla zuzustellen.</p><p>10.0080</p><p class="MsoNormal">text text text</p><p class="p--heading-2"><span class="anchor--on anchorname--160p001200">Schriftliche Bearbeitung</span</p><p>1.02</p><p>Eine blablabla text text</p><p>1.010</p><p>Ein text text (look <a xlink:type="simple" xlink:show="replace" xlink:role="17160" xlink:actuate="onRequest" xlink:href="link/a1000-text.xml">10.0060</a>) text text text</p> ..."

Now I want edit a part of string (c#) -> for example:

myText = myText.Replace("<p class="p--heading-2"><span class="anchor--on anchorname--160p00">Schriftliche Bearbeitung</span</p>", "<h2><a name="anchorname">Schriftliche Bearbeitung</a></p>");

The problem are the variable values (for excample the anchorname needs different values) and so I can´t replace the string.

Comment to first answer: I don´t want to use third-party supplier software (respective HtmlAgilityPack).

Are there any ideas for solution? If a regex the best solution, how the regex looks like? thanks.

share|improve this question
1  
I can't see at all how you got from the first string to the second. Where does ankername come from? Why <h3>? Plus, of course, you really don't want to use regex to parse HTML. –  Tim Pietzcker 22 hours ago
 
what are undefined signs..and why use regex for this –  Anirudh 22 hours ago
1  
As a general rule do not use regex to parse html –  Luis Filipe 19 hours ago

1 Answer

Use HtmlAgilityPack not regex

var doc = new HtmlDocument();
doc.LoadHtml(html);

var nodes = doc.DocumentNode.SelectNodes("//p[@class='p--heading-2']");
foreach (HtmlNode htmlNode in nodes)
{
    var newNodeStr = htmlNode.InnerText;
    var newNode = HtmlNode.CreateNode("<h3><a>"+newNodeStr+"</a></h3>");
    htmlNode.ParentNode.ReplaceChild(newNode, htmlNode);
}
share|improve this answer
 
Thanks for your answer :) Unfortunately I'm not sure whether the way is helpful. Could you please explain how I can use the "HtmlDocument" with a string? Which namespace is "HtmlDocument"? –  Rotaney 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.