0

さまざまなソースからの html をフォーマットするMetro アプリを使用しているため、html 構造に一貫性がありません。幸いなことに、この問題を解決できると思われるMetro AppsHtmlAgilityPack用のビルドがあります。

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 タグがないことを示しました。

4

1 に答える 1

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);
于 2013-06-15T16:13:24.737 に答える