修改文本 XmlNode 的 InnerXml
我使用 SGML 和 XmlDocument 遍历 html 文档。当我找到一个类型为 Text 的 XmlNode 时,我需要更改其具有 xml 元素的值。我无法更改 InnerXml,因为它是只读的。我尝试更改 InnerText,但这次标记描述符字符 <
和 >
编码为 <
和 & >
。例如:
<p>
This is a text that will be highlighted.
<anothertag />
<......>
</p>
我正在尝试更改为:
<p>
This is a text that will be <span class="highlighted">highlighted</span>.
<anothertag />
<......>
</p>
修改文本 XmlNode 的值的最简单方法是什么?
I traverse an html document with SGML and XmlDocument. When I find an XmlNode which its type is Text, I need to change its value that has an xml element. I can't change InnerXml because it's readonly. I tried to change InnerText, but this time tag descriptor chars <
and >
encoded to <
and >
. for example:
<p>
This is a text that will be highlighted.
<anothertag />
<......>
</p>
I'm trying to change to:
<p>
This is a text that will be <span class="highlighted">highlighted</span>.
<anothertag />
<......>
</p>
What is the easiest way to modify the value of a text XmlNode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个解决方法,我不知道这是一个真正的解决方案还是什么,但它可以产生我想要的结果。请评论此代码是否值得解决方案此外
,我在遍历函数之后删除
和字符串:
I have a workaround, I don't know it is a real solution or what, but it can result what I want. Please comment for this code if it is worthy solution or not
Also, I remove
<text>
and</text>
strings after the traverse function:InnerText
属性 将为您提供XmlNode
。您真正想要设置的是InnerXml< /code> 属性
,它将被解释为 XML,而不是文本。
The
InnerText
property will give you the text content of all the child nodes of theXmlNode
. What you really want to set is theInnerXml
property, which will be construed as XML, not as text.