4

我有几千(ASP.net - 混乱的 html)html 生成的发票,我试图解析并保存到数据库中。

基本上像:

 foreach(var htmlDoc in HtmlFolder)
 {
   foreach(var inputBox in htmlDoc)
   { 
      //Make Collection of ID and Values Insert to DB
   }
 }  

从我读过的所有其他问题中,解决此类问题的最佳工具是HtmlAgilityPack,但是在我的一生中,我无法使文档 .chm 文件正常工作。关于我如何在有或没有敏捷包的情况下完成此任务的任何想法?

提前致谢

4

4 回答 4

4

HtmlAgilityPack 的更新替代品是CsQuery。关于它的相对性能优点,请参见后面的问题,但它对 CSS 选择器的使用是无可匹敌的:

var doc = CQ.CreateDocumentFromFile(htmldoc); //load, parse the file
var fields = doc["input"]; //get input fields with CSS
var pairs = fields.Select(node => new Tuple<string, string>(node.Id, node.Value()))
       //get values
于 2013-12-17T17:55:45.870 回答
2

要使 CHM 工作,您可能需要在 Windows 资源管理器中查看属性并取消选中“取消阻止内容”复选框

当您熟悉 Linq-to-XML 或 XPath 时,HTML Agility Pack 非常简单。

您需要了解的基础知识:

//import the HtmlAgilityPack
using HtmlAgilityPack;

HtmlDocument doc = new HtmlDocument();

// Load your data
// -----------------------------
// Load doc from file:
doc.Load(pathToFile);

// OR

// Load doc from string:
doc.LoadHtml(contentsOfFile);
// -----------------------------

// Find what you're after
// -----------------------------
// Finding things using Linq
var nodes = doc.DocumentNode.DescendantsAndSelf("input")
    .Where(node => !string.IsNullOrWhitespace(node.Id)
        && node.Attributes["value"] != null
        && !string.IsNullOrWhitespace(node.Attributes["value"].Value));

// OR

// Finding things using XPath
var nodes = doc.DocumentNode
    .SelectNodes("//input[not(@id='') and not(@value='')]");
// -----------------------------


// looping through the nodes:
// the XPath interfaces can return null when no nodes are found
if (nodes != null) 
{ 
    foreach (var node in nodes)
    {
        var id = node.Id;
        var value = node.Attributes["value"].Value;
    }
}

添加 HtmlAgility Pack的最简单方法是使用 NuGet :

PM> Install-Package HtmlAgilityPack

于 2013-12-17T17:47:25.453 回答
1

哈,看起来是我编写的库的无耻插件的理想时机!

使用这个库应该很容易实现(顺便说一下,它构建在 HtmlAgility 包之上!):https ://github.com/amoerie/htmlbuilders (您可以在此处找到 Nuget 包:https://www. nuget.org/packages/HtmlBuilders/

代码示例:

        const string html = "<div class='invoice'><input type='text' name='abc' value='123'/><input id='ohgood' type='text' name='def' value='456'/></div>";
        var htmlDocument = new HtmlDocument {OptionCheckSyntax = false}; // avoid exceptions when html is invalid
        htmlDocument.Load(new StringReader(html));
        var tag = HtmlTag.Parse(htmlDocument); // if there is a root tag
        var tags = HtmlTag.ParseAll(htmlDocument); // if there is no root tag

        // find looks recursively through the entire DOM tree
        var inputFields = tag.Find(t => string.Equals(t.TagName, "input"));

        foreach (var inputField in inputFields)
        {
            Console.WriteLine(inputField["type"]);
            Console.WriteLine(inputField["value"]);
            if(inputField.HasAttribute("id"))
                Console.WriteLine(inputField["id"]);
        }

请注意,如果该字段没有指定的属性名称,则 inputField[attribute] 将抛出“KeyNotFoundException”。这是因为 HtmlTag 为其属性实现并重用了 IDictionary 逻辑。

编辑:如果您不在 Web 环境中运行此代码,则需要添加对 System.Web 的引用。这是因为这个库使用了 System.Web 中的 HtmlString 类。只需选择“添加引用”,然后您可以在“程序集 > 框架”下找到它

于 2013-12-17T17:50:30.960 回答
0

您可以从这里下载HtmlAgilityPack Documents CHM 文件。

如果 chm 文件内容不可见,则取消选中Always ask before opening this file复选框,如屏幕截图所示

在此处输入图像描述

注意:对于未签名的文件,会出现上述对话框

在此处输入图像描述

来源:HtmlAgilityPack 文档

于 2015-08-21T02:15:11.910 回答