使用选定的 Xpath 和 for 循环删除 XML
我目前有以下代码:
XPathNodeIterator theNodes = theNav.Select(theXPath.ToString());
while (theNodes.MoveNext())
{
//some attempts i though were close
//theNodes.RemoveChild(theNodes.Current.OuterXml);
//theNodes.Current.DeleteSelf();
}
我已将 xpath 设置为我想要在 xml 中返回的内容,并且我想删除循环的所有内容。我尝试了几种删除信息的方法,但它不喜欢我的语法。我在 Microsoft 支持上找到了一个示例:http://support.microsoft.com/kb/317666但我想用这个 while 而不是 foreach 。
如有任何意见或问题,我们将不胜感激。
I currently have the following code:
XPathNodeIterator theNodes = theNav.Select(theXPath.ToString());
while (theNodes.MoveNext())
{
//some attempts i though were close
//theNodes.RemoveChild(theNodes.Current.OuterXml);
//theNodes.Current.DeleteSelf();
}
I have set xpath to what I want to return in xml and I want to delete everything that is looped. I have tried a few ways of deleting the information but it does't like my syntax. I found an example on Microsoft support: http://support.microsoft.com/kb/317666 but I would like to use this while instead of a for each.
Any comments or questions are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用
XDocument
?结果
将为
。或者:
[编辑] 对于 .NET 2,您可以使用
XmlDocument
:[EDIT]
如果需要删除所有子元素和属性:
如果您需要保留属性,但删除元素:
Why not to use
XDocument
?result
will be<Elements><Element2 /></Elements>
.Or:
[Edit] For .NET 2, you can use
XmlDocument
:[EDIT]
If you need to remove all child elements and attributes:
If you need to keep attributes, but delete elements:
您可以使用
XmlDocument
:You can use
XmlDocument
: