正如其他人所提到的,你可以使用HtmlAgilityPack。
以下是一个例子:
DTO:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public decimal Amount { get; set; }
// etc
}
用法:
var data = @"
br
{mso-data-placement:same-cell;}
# | Asset Manager | Billing Address | Billing City | Billing State | Billing Zip Code | Contract Amount | DUNS Number | FEIN |
1 | Jim Bob | 2500 N. Park Pkwy, Suite 600 | Plano | Texas | 75093 | $0.00 |
";
string xmlData;
// Create the document
var document = new HtmlDocument();
document.LoadHtml(data);
document.OptionOutputAsXml = true;
// Write it to Xml so we can use XDocument Linq
using(var stream = new StringWriter())
using(var writer = XmlWriter.Create(stream))
{
document.Save(writer);
xmlData = stream.ToString();
}
// Use XDocument Linq to parse the xml into Customer objects
var customers =
XDocument.Parse(xmlData)
.XPathSelectElements("//table/tr")
.Skip(1)
.Select(x => new Customer {
Id = int.Parse(x.Elements("td").First().Value),
Name = x.Elements("td").Skip(1).First().Value,
Address= x.Elements("td").Skip(2).First().Value,
City = x.Elements("td").Skip(3).First().Value,
State = x.Elements("td").Skip(4).First().Value,
Zip = x.Elements("td").Skip(5).First().Value,
Amount = decimal.Parse(x.Elements("td").Skip(6).First().Value,
System.Globalization.NumberStyles.Currency)
});