繁体   English   中英

Ct:HtmlAgilityPack后代

[英]c#: HtmlAgilityPack Descendants

美好的一天。 我有一个需要将Word文档转换为html的任务。

可以使用互操作来完成此操作,并将文档另存为html。 但是我需要清理互操作的html输出

但是我对htmlagilitypack有问题。 我认为它类似于XmlDocument c#

这是我的C#代码

HtmlDocument doc = new HtmlDocument();
doc.Load(htmlLocation);
      foreach (var item in doc.DocumentNode.Descendants("p"))
      {

      if (item.HasChildNodes)
          {
             foreach (var itm in item.Descendants("span").ToList())
                {
                   Console.WriteLine(itm.InnerText);
                }
          }
      }

这是HTML代码

<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 12 (filtered)">

</head>

<body lang=EN-US link="#0066CC" vlink=purple style='text-justify-trim:punctuation'>

<div class=WordSection1>

<p class=Heading61 style='margin-bottom:0in;margin-bottom:.0001pt;text-indent:
.5in;line-height:normal;page-break-after:avoid;background:transparent'><span
class=Heading6><span style='font-size:12.0pt;color:black;background:yellow'>Epilogue</span></span></p>

<p class=MsoBodyText style='line-height:normal;background:transparent'><span
class=BodytextItalic2><span style='font-size:12.0pt;color:black;font-style:
normal'>&nbsp;</span></span></p>

<p class=MsoBodyText style='line-height:normal;background:transparent'><span
class=BodytextItalic2><span style='font-size:12.0pt;color:black;font-style:
normal'>Rebecca sat outside her lodge cradling her infant son in her arms. How
handsome he was, her little warrior, with his dusky skin and thick black hair.
For the first few days after his birth, she had been afraid to let him out of
her sight, out of her arms, for fear she would lose him, but he was a strong
healthy child.</span></span></p>

<p class=MsoBodyText style='text-indent:.5in;line-height:normal;background:
transparent'><span class=BodytextItalic2><span style='font-size:12.0pt;
color:black;font-style:normal'>Looking at him made her heart swell with love
for him and for his father. She had married Wolf Dreamer the day after they
returned to his people. Summer Moon Rising had left the village the following
day.</span></span></p>

</div>

</body>

</html>

这是上面代码的输出

Epilogue
Epilogue
&nbsp;
&nbsp;
Rebecca sat outside her lodge cradling her infant son in her arms. How
handsome he was, her little warrior, with his dusky skin and thick black hair.
For the first few days after his birth, she had been afraid to let him out of
her sight, out of her arms, for fear she would lose him, but he was a strong
healthy child.
Rebecca sat outside her lodge cradling her infant son in her arms. How
handsome he was, her little warrior, with his dusky skin and thick black hair.
For the first few days after his birth, she had been afraid to let him out of
her sight, out of her arms, for fear she would lose him, but he was a strong
healthy child.
Looking at him made her heart swell with love
for him and for his father. She had married Wolf Dreamer the day after they
returned to his people. Summer Moon Rising had left the village the following
day.
Looking at him made her heart swell with love
for him and for his father. She had married Wolf Dreamer the day after they day.

我期望的是每个第二个取决于项目元素。 但是为什么要重复文本?

您有4个p标签,每个标签有两个跨度。 后代,获取具有匹配名称的所有后代节点,以便您的内部foreach重复两次

你内心的foreach可能是

    foreach (var itm in item.ChildNodes)
    {
      Console.WriteLine(itm.InnerText);
    }

暂无
暂无

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

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