html到c#對象,遞歸函數?

c# dom html html-agility-pack object

我正在嘗試將html文檔轉換為c#對象。我有一個有序列表中的名稱示例列表,如下所示。我正在使用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>

這些是我創建的用於表示名稱列表的對象。即人和他們的孩子。

<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>

我在想創建這些對象,遞歸函數最好。任何人都可以提供有關如何將html轉換為c#對象的任何想法嗎?

阿布。

一般承認的答案

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

var list = Recurse(doc.DocumentNode);

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

var list = Recurse(doc.DocumentNode);

熱門答案

為了好玩 - 或者如果你真的想要一個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();
        }
    }
}

您可能想要的(猜測您沒有指定)是您可以使用的人員及其子女列表:

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

如果您想(或需要)使用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();
        }
    }
}



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因