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를 PersonList와 Person 목록이 실제로 원하는 경우에는 다음과 같이 할 수 있습니다 (게시 한 코드에 대해 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는 합법적입니까? 예, 이유를 알아보십시오.