1

I'm trying to convert a html document to c# object. I have a example list of names in an ordered list as below. I am using Html Agility Pack.

<ol>
    <li>Heather</li>
    <li>Channing</li>
    <li>Briana</li>
    <li>Amber</li>
    <li>Sabrina</li>
    <li>Jessica
        <ol>
            <li>Melody</li>
            <li>Dakota</li>
            <li>Sierra</li>
            <li>Vandi</li>
            <li>Crystal</li>
            <li>Samantha</li>
            <li>Autumn</li>
            <li>Ruby</li>
        </ol></li>
    <li>Taylor</li>
    <li>Tara</li>
    <li>Tammy</li>
    <li>Laura</li>
    <li>Shelly</li>
    <li>Shantelle</li>
    <li>Bob and Alice
      <ol>
        <li>Courtney</li>
        <li>Misty</li>
        <li>Jenny</li>
        <li>Christa</li>
        <li>Mindy</li>
      </ol></li>
    <li>Noel</li>
    <li>Shelby</li>
</ol>

These are the objects I have created to represent the list of names. I.e. People and their children.

public class PeopleList {
    public List<Person> People {get; set;}
}

public class Person {
    public string Name {get; set;}
    public PeopleList Children {get; set;}
}

I was thinking that to create these objects, a recursive function would be best. Can anyone offer any ideas about how to go about converting the html into c# objects?

Abu.

Abu Ragneesh
  • 63
  • 1
  • 5
  • just use xpath to grab the part of the document and then use recursive function to return nested list – Paul Sullivan Dec 23 '12 at 10:10
  • Do you have any examples? I've been trying to use a recursive function, but having trouble figuring it out – Abu Ragneesh Dec 23 '12 at 10:16
  • How do you want to populate it a) a PeopleList of Persons having children of type Person or b) a List of People with their PeopleList? – Anthill Dec 23 '12 at 12:00

3 Answers3

3
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var list = Recurse(doc.DocumentNode);

List<Person> Recurse(HtmlAgilityPack.HtmlNode root)
{
    var ol = root.Element("ol");
    if (ol == null) return null;

    return ol.Elements("li")
                .Select(li => new Person
                {
                    Name = li.FirstChild.InnerText.Trim(),
                    Children = Recurse(li)
                })
                .ToList();
}
L.B
  • 114,136
  • 19
  • 178
  • 224
0

I would look into the HTMLAgilityPack http://htmlagilitypack.codeplex.com/

I haven't use it for this specifically but it works really well for parsing HTML.

whoacowboy
  • 6,982
  • 6
  • 44
  • 78
  • I'm using HtmlAgilityPack already. Just trying to figure out how to parse this list of names and children into c# objects. – Abu Ragneesh Dec 23 '12 at 10:14
0

For fun - or in case you really want a List of PeopleList having Persons with their PeopleList :p - you could do something like (no need for HtmlAgilityPack for the code you posted):

namespace StackFun
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;

    public class PeopleList
    {
        public List<Person> People { get; set; }
    }

    public class Person
    {
        public string Name { get; set; }
        public PeopleList Children { get; set; }
    }

    class Program
    {
        static IEnumerable<PeopleList> GetChildren(PeopleList parent, IEnumerable<XElement> children)
        {
            parent.People = new List<Person>();
            foreach (var child in children)
            {
                var person = new Person
                {
                    Name = ((XText)child.FirstNode).Value.Trim(new[] { ' ', '\r', '\n' }),
                };
                parent.People.Add(person);
                foreach (var childrenOf in child.Elements("ol").SelectMany(BuildFromXml))
                {
                    person.Children = childrenOf;
                }
            }
            yield return parent;

        }

        static IEnumerable<PeopleList> BuildFromXml(XElement node)
        {
            return GetChildren(new PeopleList(), node.Elements("li"));
        }

        static void Main(string[] args)
        {
            const string xml = @"<ol>
            <li>Heather</li>
            <li>Channing</li>
            <li>Briana</li>
            <li>Amber</li>
            <li>Sabrina</li>
            <li>Jessica
                <ol>
                    <li>Melody</li>
                    <li>Dakota</li>
                    <li>Sierra</li>
                    <li>Vandi</li>
                    <li>Crystal</li>
                    <li>Samantha</li>
                    <li>Autumn</li>
                    <li>Ruby</li>
                </ol></li>
            <li>Taylor</li>
            <li>Tara</li>
            <li>Tammy</li>
            <li>Laura</li>
            <li>Shelly</li>
            <li>Shantelle</li>
            <li>Bob and Alice
              <ol>
                <li>Courtney</li>
                <li>Misty</li>
                <li>Jenny</li>
                <li>Christa</li>
                <li>Mindy</li>
              </ol></li>
            <li>Noel</li>
            <li>Shelby</li>
        </ol>";

            var doc = XDocument.Parse(xml);
            var listOfPeople = BuildFromXml(doc.Root).ToList();
        }
    }
}

What you probably want though (guessing you haven't specified) is a List of People and their Children which you could get using:

static IEnumerable<Person>Populate(IEnumerable<XElement> children)
{
     foreach (var child in children)
     {
          var person = new Person
          {
              Name = ((XText)child.FirstNode).Value.Trim(new[] { ' ', '\r', '\n' }),
              Children = new PeopleList()

           };
           person.Children.People = new List<Person>();
           foreach (var childrenOf in child.Elements("ol").SelectMany(BuildFromXml))
           {
               person.Children.People.Add(childrenOf);
           }
           yield return person;
     }

}

static IEnumerable<Person> BuildFromXml(XElement node)
{
    return Populate(node.Elements("li"));
}

And if you want (or need) to use HtmlAgilityPack the code could look like:

class Program
{
    static IEnumerable<Person> Populate(IEnumerable<HtmlNode> children)
    {
        foreach (var child in children)
        {
            var person = new Person
            {
                Name = child.InnerText.Split(new char[] { '\r', '\n' })[0].Trim(),
                Children = new PeopleList()

            };
            person.Children.People = new List<Person>();
            foreach (var childrenOf in child.Elements("ol").SelectMany(BuildFromHtml))
            {
                person.Children.People.Add(childrenOf);
            }
            yield return person;
        }


    }
    static IEnumerable<Person> BuildFromHtml(HtmlNode node)
    {
        return Populate(node.Elements("li"));
    }

    static void Main(string[] args)
    {
        const string html = @"<ol>
            <li>Heather</li>
            <li>Channing</li>
            <li>Briana</li>
            <li>Amber</li>
            <li>Sabrina</li>
            <li>Jessica
                <ol>
                    <li>Melody</li>
                    <li>Dakota</li>
                    <li>Sierra</li>
                    <li>Vandi</li>
                    <li>Crystal</li>
                    <li>Samantha</li>
                    <li>Autumn</li>
                    <li>Ruby</li>
                </ol></li>
            <li>Taylor</li>
            <li>Tara</li>
            <li>Tammy</li>
            <li>Laura</li>
            <li>Shelly</li>
            <li>Shantelle</li>
            <li>Bob and Alice
              <ol>
                <li>Courtney</li>
                <li>Misty</li>
                <li>Jenny</li>
                <li>Christa</li>
                <li>Mindy</li>
              </ol></li>
            <li>Noel</li>
            <li>Shelby</li>
        </ol>";

        var doc = new HtmlDocument();
        doc.LoadHtml(html);
        var listOfPeople = BuildFromHtml(doc.DocumentNode.FirstChild).ToList();
    }
}
Anthill
  • 1,219
  • 10
  • 20