如何解析HTML节点

时间:2016-12-16 11:54:59

标签: c# asp.net asp.net-mvc openxml

我的网站流程。

  1. 经过身份验证的用户将上传docx。
  2. 我正在使用OpenXmlPowerTools API将此docx转换为HTML
  3. 保存文件
  4. 将html页面的每个节点保存到数据库中。
  5. 数据库: -

    tblNodeCollection
    
    • 的NodeId
    • 节点类型(预期值 - <p><h1><h3><table>
    • NodeContent(预期值 - <p> This is p content </p>

    直到步骤#3 才会出现问题。但我 无能 关于如何将节点集合保存到表中。

    我用google搜索&amp;发现HTMLAgiiltiyPack但对此并不了解。

    using DocumentFormat.OpenXml.Packaging;
    using HtmlAgilityPack;
    using OpenXmlPowerTools;
    
    namespace ExportData 
    {
    public class ExportHandler 
    {
    public void GenerateHTML()
        {
            byte[] byteArray = File.ReadAllBytes(@"d:\test.docx");
            using (MemoryStream memoryStream = new MemoryStream())
            {
                memoryStream.Write(byteArray, 0, byteArray.Length);
                using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(memoryStream, true))
                {
                    HtmlConverterSettings settings = new HtmlConverterSettings()
                    {
                        PageTitle = "My Page Title"
                    };
                    XElement html = HtmlConverter.ConvertToHtml(doc, settings);
    
                    File.WriteAllText(@"d:\Test.html", html.ToStringNewLineOnAttributes());
    
    
                }
            }
    
            //now how do I proceed from here
        }
     }
    

    高度赞赏任何类型的帮助/指导。

2 个答案:

答案 0 :(得分:0)

根据我们在评论中所讨论的内容以及您似乎坚持的部分,我建议如下:

这里的Question可以为如何转换为html提供一些帮助。

当然,您仍然面临需要能够拆分每个页面的问题(正如您在评论中提到的那样),您可以能够将每个页面单独导出为html。

至于你的数据库结构,我建议类似于:

[Document Table]
  - Document ID
  - Document Name
  - Any other data you need per-document

[Node Table]
  - Node ID
  - Document ID (foreign key)
  - Node Content (string)

确保您在节点表上有合理的索引,因为随着时间的推移,您可能会搜索成千上万行(特别是文档ID上的一行)。

对每个节点(例如bigint位置)使用索引属性也可能很有用,这样您就可以按顺序将节点重新组合在一起来重新构建文档。

总的来说,我的建议是尝试让你的老板看到理由并真正反对这个愚蠢的设计决定。

答案 1 :(得分:0)

以下是如何解析html并将其保存到数据库的简化过程。我希望这能帮助你和/或让你知道如何解决你的问题

        HtmlWeb h = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument doc = h.Load("http://stackoverflow.com/questions/41183837/how-to-store-html-nodes-into-database");
        HtmlNodeCollection tableNodes = doc.DocumentNode.SelectNodes("//table");
        HtmlNodeCollection h1Nodes = doc.DocumentNode.SelectNodes("//h1");
        HtmlNodeCollection pNodes = doc.DocumentNode.SelectNodes("//p");
        //get other nodes here

        foreach (var pNode in pNodes)
        {
            string id = pNode.Id;
            string content = pNode.InnerText;
            string tag = pNode.Name;

            //do other stuff here and then save to database

            //just an example...
            SqlConnection conn = new SqlConnection("here goes conection string");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO tblNodeCollection (Tag, Id, Content) VALUES (@tag, @id, @content)";
            cmd.Parameters.Add("@tag", tag);
            cmd.Parameters.Add("@id", id);
            cmd.Parameters.Add("@content", content);

            cmd.ExecuteNonQuery();
        }
相关问题