Split String Html

Asked

Viewed 306 times

0

I have an HTML with the following code:

<div class="page">
...Conteúdo1
</div>

<div class="page">
...Conteúdo2
</div>

<div class="page">
...Conteúdo3
</div>

the class "page" it creates a background as if it were an A4 sheet, so that the content is placed inside it, the following happens, that when I bring the whole HTML in string they see in a single string, only that I needed for each div of class "page" to generate an image individually, I just have no idea how I could possibly separate each div into a different string. If anyone can help.

string HTMLemString = RenderizaHtmlComoString("~/Views/Item/Item.cshtml", id);

the code above is where it brings all the html of the page.

  • But how do you currently do it? How is the code that generates this HTML page with Divs?

  • I put the code in the question.

  • You’re still confused. How do you know how many Ivs you need? Why are you calling your View this way RenderizaHtmlComoString("~/Views/Item/Item.cshtml", id);? You will probably need to make several changes, but in Item.cshtml you can create a loop that creates the Divs, as long as you inform somehow the amount you want.

  • puts the value that the string HTMLemString stay after you run that call HTMLemString = RenderizaHtmlComoString("~/Views/Item/Item.cshtml", id);

2 answers

0

Well, maybe this hiccup is a little forced, but it works..

        string split = "<div class=\'page\'>";
        List<string> div = System.Text.RegularExpressions.Regex.Split(HTMLemString, split, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Where(w=>!string.IsNullOrEmpty(w)).Select(s=> split + s).ToList();
  • attention as '... you may have to switch to " to coincide with what you had

0


this code does the magic using regular expressions

var texto = "<div class='page'>...Conteúdo1</div>";
texto += "<div class='page'>...Conteúdo2</div>";
texto += "<div class='page'>...Conteúdo3</div>";
string pattern = "<div class='page'>(.*?)<\\/div>";
MatchCollection matches = Regex.Matches(texto, pattern);

foreach (Match m in matches)
{
    MessageBox.Show(m.ToString());
}

each iteration of the foreach loop is a 'page'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.