Metro App htmllagilitypack构建格式良好的Html
本文关键字:Html 格式 App htmllagilitypack 构建 Metro | 更新日期: 2023-09-27 18:18:44
我有一个Metro App,从各种来源格式化html,因此在html结构中没有任何一致性。幸运的是,有一个HtmlAgilityPack
构建Metro Apps我认为可以帮助解决这个问题。
我正在努力确保所有的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>
SOLUTION不应该专门针对上述html进行编码。我只是用示例html来演示,它没有html, head或body标签
操作如下:
// 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);