Metro App HtmlAgilityPack构建格式良好的Html

时间:2013-06-15 15:14:15

标签: c# windows-runtime microsoft-metro .net-4.5 html-agility-pack

我有一个 Metro App ,可以从各种来源格式化html,因此html结构没有任何一致性。幸运的是, Metro Apps HtmlAgilityPack版本,我认为这可以帮助解决这个问题。

我正在努力确保所有HTML符合此标准:

<html>
<head>
    ...
</head>
<body>
    ...
</body>
</html>

你为什么这么问?我想使用CSS3转换/动画,这需要我

  • HEAD
  • 中添加一些样式
  • 订阅BODY onload活动。

我对源html的问题在于:

  • 有时会包含HTML标记。
  • 有时会包含HEAD标记。
  • 有时会包含BODY标记。

这是我到目前为止所做的:

            // Load the html
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.OptionFixNestedTags = true;
            htmlDocument.LoadHtml(html);

            // Ensure that the html node exists
            HtmlNode htmlNode = htmlDocument.DocumentNode.Element("html");
            if (htmlNode == null)
            {
                htmlNode = HtmlNode.CreateNode("html");
                htmlDocument.DocumentNode.AppendChild(htmlNode);
            }

            // Ensure that the head node exists
            HtmlNode headNode = htmlNode.Element("head");
            if (headNode == null)
            {
                headNode = HtmlNode.CreateNode("head");
                htmlNode.AppendChild(htmlNode);
            }

            // Ensure that the body node exists
            HtmlNode bodyNode = htmlNode.Element("body");
            if (bodyNode == null)
            {
                bodyNode = HtmlNode.CreateNode("body");
                htmlNode.AppendChild(bodyNode);
            }

这就是我所坚持的:

  • 现在已经有了一些结构,我如何找到并移动不应该在HTML或HEAD标签中的所有标签,并将它们移动到BODY标签中。

这是一个格式错误的HTML示例:

<a href="http://www.somewhere.co.za/" target="_blank"> Somewhere (Pty) Ltd</a><br><br>
Hello Anonymous!, <br>
Good news! You order has been shipped. <br>
Order Number: 108<br>
Order Details: <a href="http://somewhere.co.za/orderdetails/108" target="_blank">http://somewhere.co.za/orderdetails/108</a><br>
Date Ordered: 14 June 2013<br><br><br><br>
<table border="0" style="width:100%;">
<tr style="background-color:#b9babe;text-align:center;">
<th>Name</th>
<th>Quantity</th>
</tr>
<tr style="background-color: #ebecee;text-align: center;">
<td style="padding: 0.6em 0.4em;text-align: left;">Non Branded - Ladies - Batwing Sleeves High Elastic Loose (Non Branded - Ladies - Batwing Sleeves High Elastic Loose - Grey)
<br>
Size: Free Size
<br>
SKU: NBLBSHELGY
</td>
<td style="padding: 0.6em 0.4em;text-align: center;">1</td>
</tr>
</table>

解决方案不应该针对上面的html编码。我只是用示例html进行演示,它没有html,head或body标签。

1 个答案:

答案 0 :(得分:0)

按照以下方式工作:

            // Load the html
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.OptionFixNestedTags = true;
            string html = (message.TextContentType == ETextContentType.Html ? message.Text : string.Format("<p>{0}</p>", (message.Text + string.Empty).Replace(Environment.NewLine, "<br/>")));
            htmlDocument.LoadHtml(html);

            // Ensure that the html node exists
            HtmlNode htmlNode = htmlDocument.DocumentNode.Descendants("html").FirstOrDefault();
            if (htmlNode == null)
            {
                htmlNode = htmlDocument.CreateElement("html");
                htmlDocument.DocumentNode.AppendChild(htmlNode);
            }

            // Ensure that the head node exists
            HtmlNode headNode = htmlDocument.DocumentNode.Descendants("head").FirstOrDefault();
            if (headNode == null)
            {
                headNode = htmlDocument.CreateElement("head");
                htmlNode.AppendChild(headNode);
            }

            // Create page css transition
            HtmlNode cssTransitionNode = htmlDocument.CreateElement("style");
            cssTransitionNode.InnerHtml = "body{opacity:0;transition: all 2s ease;}.loaded{opacity:1;}";
            headNode.PrependChild(cssTransitionNode);

            // Create page javascript transition
            HtmlNode javascriptTransitionNode = htmlDocument.CreateElement("script");
            javascriptTransitionNode.Attributes.Add("type", "text/javascript");
            javascriptTransitionNode.InnerHtml = "document.addEventListener('DOMContentLoaded', function () { document.body.classList.add('loaded'); }, false);";
            headNode.AppendChild(javascriptTransitionNode);

            // Ensure that the body node exists
            HtmlNode bodyNode = htmlDocument.DocumentNode.Descendants("body").FirstOrDefault();
            if (bodyNode == null)
            {
                bodyNode = htmlDocument.CreateElement("body");
                htmlNode.AppendChild(bodyNode);
            }

            // Add the body tags
            HtmlNodeCollection htmlNodes = new HtmlNodeCollection(bodyNode);
            foreach (HtmlNode node in htmlDocument.DocumentNode.ChildNodes.ToList())
            {
                if (!node.Name.Equals("html", StringComparison.OrdinalIgnoreCase)
                 && !node.Name.Equals("head", StringComparison.OrdinalIgnoreCase)
                 && !node.Name.Equals("body", StringComparison.OrdinalIgnoreCase))
                {
                    htmlNodes.Add(node);
                    htmlDocument.DocumentNode.RemoveChild(node);
                }
            }
            bodyNode.AppendChildren(htmlNodes);