2016-12-16 95 views
1

我的网站流量。如何解析HTML节点

  1. 通过身份验证的用户将上传docx。
  2. 我使用OpenXmlPowerTools API转换这个DOCX为HTML
  3. 保存文件
  4. 保存HTML页面的各个节点到数据库中。

数据库: -

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

直到步骤#3没有问题。但我是无知关于如何将节点集合保存到表中。

我使用谷歌搜索&找到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 
    } 
} 

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

+1

我们可以问*为什么*你试图将节点保存在数据库中?为什么不保存整个XML并在需要时解析并处理它? – Clint

+0

@Clint No.
该网站有很多其他的东西与每个节点。 –

+0

任何可能的背景?它可能完全决定最佳解决方案。 – Clint

回答

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(); 
     } 
+0

您的文章似乎与我的需求高度相关。让我进一步检查一下。谢谢:) –

+0

是的。它满足了我的需求。感谢:) –

+0

这是一个upwote,但它似乎真正的问题是如何使用HtmlAgilityPack来解析HTML :) – Nino

0

从讨论中,我们已经在意见,好像你的一部分被卡住,我想提出以下建议:

这里这Question上SO可以提供一些帮助,如何转换为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位置)也是有用的,因此您可以通过按顺序将节点重新排列在一起来重构文档。总的来说,我的建议是尝试让你的老板看到理由,真正推动这个愚蠢的设计决定。

+0

但是,我如何分割我的HTML页面节点。这是我的疑问 –