0

我正在尝试将 html 文档转换为 c# 对象。我在有序列表中有一个示例名称列表,如下所示。我正在使用 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>

这些是我创建的用于表示名称列表的对象。即人们和他们的孩子。

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

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

我在想,要创建这些对象,最好使用递归函数。任何人都可以提供有关如何将 html 转换为 c# 对象的任何想法吗?

阿布。

4

3 回答 3

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();
}
于 2012-12-23T11:51:35.863 回答
0

我会研究 HTMLAgilityPack http://htmlagilitypack.codeplex.com/

我没有专门为此使用它,但它非常适合解析 HTML。

于 2012-12-23T10:12:21.627 回答
0

为了好玩 - 或者如果你真的想要一个包含人员列表的 PeopleList 列表:p - 你可以做类似的事情(你发布的代码不需要 HtmlAgilityPack):

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();
        }
    }
}

你可能想要的(猜你没有指定)是一个你可以使用的人员和他们的孩子的列表:

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"));
}

如果您想(或需要)使用 HtmlAgilityPack,代码可能如下所示:

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();
    }
}
于 2012-12-23T12:25:33.723 回答