【问题标题】:Metro App HtmlAgilityPack construct well formatted HtmlMetro App HtmlAgilityPack 构造格式良好的 Html
【发布时间】:2013-06-12 01:56:18
【问题描述】:

我有一个 Metro 应用程序,它可以格式化来自各种来源的 html,因此 html 结构没有任何一致性。幸运的是,Metro Apps 有一个 HtmlAgilityPack 构建,我认为它可以帮助解决这个问题。

我正在努力确保所有HTML 都符合这个标准:

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

你为什么问?我想使用CSS3 需要我这样做的过渡/动画

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

我对源 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 标记。

【问题讨论】:

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


    【解决方案1】:

    让它按如下方式工作:

                // 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      相关资源
      最近更新 更多