为了好玩 - 或者如果你真的想要一个包含人员列表的 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();
}
}