简体   繁体   English

在WinRT的HtmlAgilityPack中使用ReplaceChild

[英]Using ReplaceChild in HtmlAgilityPack in WinRT

I'm trying to convert HTML to markdown in a windows store app. 我正在尝试将HTML转换为Windows Store应用程序中的markdown。 The following code works in a .NET 4 MVC project 以下代码在.NET 4 MVC项目中有效

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html); 

foreach (var x in doc.DocumentNode.SelectNodes("//strong"))
{
    x.ParentNode.ReplaceChild(
            HtmlAgilityPack.HtmlNode.CreateNode("**" + x.InnerHtml + "**"),
    x);
}

SelectNodes is not available in the WinRT build so when porting the code I replaced this with Descendants. SelectNodes在WinRT版本中不可用,因此在移植代码时,我用Descendants替换了它。

foreach (var x in doc.DocumentNode.Descendants("//strong"))
{
    x.ParentNode.ReplaceChild(
            HtmlAgilityPack.HtmlNode.CreateNode("**" + x.InnerHtml + "**"),
    x);
}

This code leaves the HtmlDocument unchanged. 此代码使HtmlDocument保持不变。 Am I doing something wrong, or is this a bug? 我是在做错什么,还是一个错误?

Descendants expects an element name, not an XPath , therefore your code doesn't return any element and document remains unchanged. Descendants需要一个元素名称,而不是XPath ,因此您的代码不返回任何元素,并且文档保持不变。

You need to remove slashes : 您需要删除斜杠

foreach (var x in doc.DocumentNode.Descendants("strong"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM