需要删除字符串中的xml节点并保留文本 [英] need to remove xml nodes in a string and leave the text
本文介绍了需要删除字符串中的xml节点并保留文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,它是 xml 的一部分.
a<b>b</b>c<i>d</i>e<b>f</b>g
问题是我想从字符串中提取不在任何标签内的部分.所以我需要从这个字符串中提取字符串aceg"并留下字符bdf"这怎么办?
这是 xml 的一部分让我们假设它
abcdefg
现在它是一个有效的 xml :)
解决方案
该字符串不是有效的 XML.
但是,假设您有一个有效的 XML 字符串,那么您可以执行以下操作:
class 程序{static void Main(string[] args){字符串内容 = string.Empty;XmlDocument 文档 = 新 XmlDocument();document.LoadXml("<outer>a<b>b</b>c<i>d</i>e<b>f</b>g</outer>");foreach(document.DocumentElement.ChildNodes 中的 XmlNode 子节点){if (child.NodeType == XmlNodeType.Element){内容 += child.InnerText;}}Console.WriteLine(内容);Console.ReadKey();}}
这将打印出字符串bdf"
i have the string which is a part of an xml.
a<b>b</b>c<i>d</i>e<b>f</b>g
the problem is that i want to extract from the string the parts that are not inside any tags. so i need to extract the string"aceg" from this string and leave the characters "bdf" how can this be done?
Edit: this was a part of an xml let asume its
<div>a<b>b</b>c<i>d</i>e<b>f</b>g</div>
now its a valid xml :)
解决方案
That string is not valid XML.
However, assuming you had a valid XML string, then you could do something like this:
class Program
{
static void Main(string[] args)
{
string contents = string.Empty;
XmlDocument document = new XmlDocument();
document.LoadXml("<outer>a<b>b</b>c<i>d</i>e<b>f</b>g</outer>");
foreach(XmlNode child in document.DocumentElement.ChildNodes)
{
if (child.NodeType == XmlNodeType.Element)
{
contents += child.InnerText;
}
}
Console.WriteLine(contents);
Console.ReadKey();
}
}
This will print out the string "bdf"
这篇关于需要删除字符串中的xml节点并保留文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文