tidy实现html文件转化为xml(tidy.dll实现方式)(原创)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Text;
using System.Data.SqlClient;
using System.IO;

using System.Text.RegularExpressions;

 

public partial class CreateNewsXML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string path = "C:\\NewsXML";
            //美食
            writeNewsXML("269", path + "cy.xml");
            Response.Write("生成成功");

//            <a>
//<xsl:attribute name="href">
//   <xsl:value-of select="link"/>
//</xsl:attribute>
//   <xsl:value-of select="link"/>
//</a>
        }
    }


    public void writeNewsXML(string typeID,string file)
    {
        string strSql = @"select top 100 news.* where news_type" + typeID + " ";

        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
        sb.Append("<document>");
        sb.Append("<website>www.jmw.com.cn</website>");
        sb.Append("<webMaster>pc_lijiaming@jmw.com.cn</webMaster>");
        sb.Append("<updatePeri>15</updatePeri>");


        using (SqlConnection con = new SqlConnection(PriorJoin.News.DBConnect.JMWDB))
        {
            con.Open();
            SqlCommand command = new SqlCommand(strSql, con);
            command.CommandType = CommandType.Text;
            DataSet ds = new DataSet("DATASET");
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = command;
            sda.Fill(ds, "DATATABLE");

            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<item>");

                if (dt.Rows[i]["title"]!=null)
                    sb.Append("<title>" + Convert(dt.Rows[i]["title"].ToString()) + "</title>");

                if (dt.Rows[i]["filename"]!=null)
                    sb.Append("<linkURL>" + GetLinkStr(dt.Rows[i]["filename"].ToString()) + "</linkURL>");

                if (dt.Rows[i]["description"].ToString() != null)
                    sb.Append("<description>" + GetSubStr(100, dt.Rows[i]["description"].ToString()) + "</description>");

                if (dt.Rows[i]["content"]!=null)
                    sb.Append("<text>" + Convert(dt.Rows[i]["content"].ToString()) + "</text>");

                if (dt.Rows[i]["topimg"]!=null)
                    sb.Append("<image>" + dt.Rows[i]["topimg"] + "</image>");

                if (dt.Rows[i]["sname"]!=null)
                    sb.Append("<category>" + dt.Rows[i]["sname"] + "</category>");

                if (dt.Rows[i]["uptime"]!=null)
                    sb.Append("<pubDate>" + DateTime.Parse(dt.Rows[i]["uptime"].ToString()).ToString("yyyy-MM-dd HH:mm") + "</pubDate>");

                if (dt.Rows[i]["author"]!=null)
                    sb.Append("<author>" + dt.Rows[i]["author"] + "</author>");

                sb.Append("</item>");
            }

        }

        sb.Append("</document>");

 

//tidy实现补全或剔除html特殊字符
        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.OptionOutputAsXml = true;
        htmlDoc.LoadHtml(sb.ToString());
        StringBuilder sbXml = new StringBuilder();
        StringWriter sw = new StringWriter(sbXml);
        htmlDoc.Save(sw);

//写入文件
        WriteFile(file, sbXml.ToString());
    }
   

    protected bool WriteFile(string path, string _value)
    {
        StreamWriter sr = new StreamWriter(path, false, Encoding.Default);
        try
        {
            sr.Write(_value.ToString());
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            sr.Close();
        }
    }

    protected string GetLinkStr(string link)
    {
        return link.Replace(".htm", ".shtml");
    }

    protected string GetSubStr(int len,string str)
    {
        if (str.Length > len)
            return str.Substring(0, len);
        return str;
    }

    public string Convert(string source)
    {
        string result;

        //remove line breaks,tabs
        result = source.Replace("\r", " ");
        result = result.Replace("\n", " ");
        result = result.Replace("\t", " ");

        //remove the header
        result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase);

        result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase);

        //remove all styles
        result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase); //clearing attributes
        result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase);

        //insert tabs in spaces of <td> tags
        result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase);

        //insert line breaks in places of <br> and <li> tags
        result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase);

        //insert line paragraphs in places of <tr> and <p> tags
        result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase);

        //remove anything thats enclosed inside < >
        result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);

        //replace special characters:
        result = Regex.Replace(result, @"&amp;", "&", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"&nbsp;", " ", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"&lt;", "<", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"&gt;", ">", RegexOptions.IgnoreCase);
        result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);

        //remove extra line breaks and tabs
        result = Regex.Replace(result, @" ( )+", " ");
        result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
        result = Regex.Replace(result, @"(\r\r)+", "\r\n");

        result = Regex.Replace(result, @"([imga]).*([/imga])", "");
        result = Regex.Replace(result, @"([img).*([/img])", "");
        result = Regex.Replace(result, @"[COLOR=red]","");
        result = Regex.Replace(result, @"[/COLOR]", "");

        return result;
    }


  
}

posted on 2010-08-25 17:25  bzqzz  阅读(592)  评论(0)    收藏  举报

导航