亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c# 提取文檔信息的示例

 更新時(shí)間:2021年02月25日 14:29:59   作者:HelloLLLLL  
這篇文章主要介紹了c# 提取文檔信息的示例,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

事情時(shí)這樣,有用友u8的字典數(shù)據(jù)的幫助文檔一份,同事需要把里面的很多張表的字典信息給提取出來(lái),然后構(gòu)成sql語(yǔ)句,插入數(shù)據(jù)庫(kù)。字典就是一張對(duì)表里的字段的一個(gè)說(shuō)明,長(zhǎng)這樣

同事一開(kāi)始是手動(dòng)復(fù)制到excel文檔在改的,他問(wèn)我有沒(méi)有什么簡(jiǎn)單的辦法,所以我就決定用代碼去實(shí)現(xiàn),把表格、表名等一些有效數(shù)據(jù)構(gòu)成對(duì)象,有了一個(gè)對(duì)象就好寫(xiě)sql了。

首先,我在百度上搜索,發(fā)現(xiàn)這個(gè)chm幫助文檔能被反編譯成html,經(jīng)過(guò)一番操作,使用windows自帶的工具 hh.exe 就可以實(shí)現(xiàn)幫助文檔的反編譯。運(yùn)行cmd,直接輸入命令就行,具體命令是這樣:

hh -decompile d:\test\help help.chm

d:\test\help是反編譯后的目錄。

反編譯之后,就會(huì)得到具體的html文檔,和js、css,長(zhǎng)這樣:

test目錄是我自己建的。

后面就是查看html源碼,分析出關(guān)鍵信息的xPath路徑該怎么寫(xiě),因?yàn)檫@里我用到了.net的一款工具專門對(duì)html操作的,叫做:HtmlAgilityPack,我的翻譯是:html敏捷開(kāi)發(fā)包,寫(xiě)xpath比寫(xiě)正則來(lái)的容易,這個(gè)包能很好的操作html的節(jié)點(diǎn),獲取html、innertext、屬性。

貼上我的關(guān)鍵方法:

public TableInfo GetTableInfo()
        {
            TableInfo tab = new TableInfo();
            HtmlDocument doc = new HtmlDocument();
            doc.Load(FullPathName, Encoding.GetEncoding("gb2312"), true);
            
            if (doc == null)
            {
                throw  new NullReferenceException(FullPathName + "\r\n沒(méi)有加載出文檔");
            }
            string pathGetTableName = "/html/head/title";
            string pathGetTableDesc = "/div/p";
            String pathGetTd = "/div/table/tr";
            var nodeTitle=doc.DocumentNode.SelectSingleNode(pathGetTableName);
            if (null != nodeTitle)
            {
                tab.TableName = nodeTitle.InnerText.Split(new char[1] { ' '})[0].Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
 
            }
 
            var nodeBody = doc.GetElementbyId("pagebody");
            var str = nodeBody.OuterHtml;
            var doc1 = new HtmlDocument();
            doc1.LoadHtml(str);
            var nodeDesc = doc1.DocumentNode.SelectSingleNode(pathGetTableDesc);
            if (null != nodeDesc)
            {
                tab.tableDescription = nodeDesc.InnerText.Split(new char[1] { ' ' })[0].Replace("\r","").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
 
            }
            
            var nodesTr = doc1.DocumentNode.SelectNodes(pathGetTd);
            if (nodesTr == null)
            {
                return tab;
            }
 
            List<TabFieldInfo> lists = new List<TabFieldInfo>();
 
            for (var i = 1; i < nodesTr.Count(); i++)
            {
                var childs = nodesTr[i].ChildNodes;
 
                if (childs == null)
                {
                    continue;
                }
                TabFieldInfo fi = new TabFieldInfo();
                if (childs.Count <= 5)
                {
                    continue;
                }
                fi.ColumnName = childs[1].ChildNodes[1].InnerText.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
                fi.Description = childs[2].InnerText.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
                fi.Datatype = childs[3].InnerText.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
                fi.Length = childs[4].InnerText.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
                fi.AllowNulls = childs[5].InnerText.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("&", "").Replace("nbsp;", "");
 
                lists.Add(fi);
            }
            tab.fields = lists;
 
            return tab;
        }

這里還出現(xiàn)一個(gè)問(wèn)題,“指定的路徑不合法”,原因是,我直接點(diǎn)擊文件右鍵-》屬性-》安全 把那里的文件路經(jīng)復(fù)制到代碼上去了,其實(shí)這樣復(fù)制,會(huì)造成路徑字符串最開(kāi)始的地方有個(gè)特殊字符,在vs里是隱藏的,后來(lái)我就復(fù)制地址欄上的路徑,就沒(méi)問(wèn)題了。

最后,需要完善的是,通過(guò)讀取目錄,把目錄中的所有html結(jié)尾的文件遍歷,并過(guò)濾出需要的表,在構(gòu)建對(duì)象。

以上就是c# 提取文檔信息的示例的詳細(xì)內(nèi)容,更多關(guān)于c# 提取文檔信息的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論