Paste HTML List Content to Text

How can I do that? The .InnerText property (when used in the containing element div) gives me only html. Is it possible to iterate over all elements liand contain their values?

+3
source share
5 answers

Is it possible to iterate over all li elements that contain and retrieve their values?

From the OP comment :

Doctype is HTML 5 and it is valid code. - Radu

In this case, you can simply use the following XPath expression :

div//li//text()

Here, all text nodes that are descendants of all elements lithat are descendants of any element divthat is a child of the current node are selected .

XPath XML ( HTML5 - XML) , , , .

:

using System;
using System.Xml;

class TestXPath
{
    static void Main(string[] args)
    {
        string html5Text =
@"<html>
 <head>
 </head>
 <body>
  <div>
   <ul>
    <li>Line 1</li>
    <li>Line 2</li>
    <li>Line 3</li>
   </ul>
  </div>
 </body>
</html>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(html5Text);

        string xpathExpr = @"/*/*/div//li//text()";

        XmlNodeList selection = doc.SelectNodes(xpathExpr);

        foreach (XmlNode node in selection)
        {
            Console.WriteLine(node.OuterXml);
        }

    }
}

, , , :

Line 1
Line 2
Line 3
+1

HTML Agilitypack, .

var nodes = doc.SelectNodes("//ul[@id=\"myId\"]/li");
List<string> items = new List<string>();
foreach (var node in nodes)
    items.Add(node.InnerText);

jquery script, ( ):

<script type="text/javascript">
    function convertUlToText(ulID)
    {
        var text = '';
        $('#' + ulID + ' li').each(function() {
          text += $(this).html() + '\r\n';
        }
        return text;
    }
</script>

:

var textList = convertUlToText('myId');
+2

, ( ), /li li :

string[] sep = {"</li>"};
foreach (string s in html.Replace("<li>","").Split(sep, StringSplitOptions.None)) 
//do something with s

:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string html = "<li>123</li><li>456</li>";
            string[] sep = {"</li>"};
            foreach (string s in html.Replace("<li>","").Split(sep, StringSplitOptions.None)) 
                Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}
+2

you can always drag and drop it with a simple one regExor break a line based on start and end tags

0
source

if possible, I would use jQuery to scroll UL and return the values ​​of each LI. Something like that ...

$('li').each(function () {

    alert($(this).html());

});

Of course, if you need these values ​​on the server side, you will need some kind of ajax call for the page method or web service.

0
source

Source: https://habr.com/ru/post/1783968/


All Articles