【发布时间】:2016-09-12 13:14:42
【问题描述】:
问题:XElement.DescendantNodes() 似乎将某些部分输出两次。
背景:
我需要将<body> 元素的全部内容复制到具有嵌入样式的<div> 内的新html 文档中。这适用于 html 邮件,其中嵌入样式应该比样式块更好,因为许多邮件代理会剥离 <head> 部分。但是,我遇到了两次获得一些零件的麻烦。如何解决这个问题?
这里是示例输入:
<body>
some text
<a href="http://www.nix.com/index.html">Click Me</a>
<br />
<span>more text</span>
</body>
这是带有重复字符串的输出,否则正是我需要的:
<body>
<div style="font-family: Verdana; font-size: 12px;">
some text
<a href="http://www.nix.com/index.html">Click Me</a>
Click Me <<<===duplicate!!!
<br />
<span>more text</span>
more text <<<===duplicate!!!
</div>
</body>
这是代码,我希望DescendantNodes() 应该是提取像<a> 这样的xelement 节点和像“一些文本”这样的文本节点的正确方法:
using System.Xml.Linq;//XElement
XElement InputMail =
new XElement("body",
"some text",
new XElement("a",
new XAttribute("href", "http://www.nix.com/index.html"),
"Click Me"),
new XElement("br"),
new XElement("span", "more text"));
XElement OutputMail =
new XElement("body",
new XElement("div",
new XAttribute("style", "font-family: Verdana; font-size: 12px;"),
InputMail.DescendantNodes()));
【问题讨论】: