删除XML使用选定XPath和一个for循环 [英] Deleting XML using a selected Xpath and a for loop

查看:260
本文介绍了删除XML使用选定XPath和一个for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下的code:

 声明XPathNodeIterator theNodes = theNav.Select(theXPath.ToString());

而(theNodes.MoveNext())
{
    //一些尝试,我虽然很接近
    //theNodes.RemoveChild(theNodes.Current.OuterXml);
    //theNodes.Current.DeleteSelf();
}
 

我已经设置的XPath什么,我想在XML返回,我想删除一个循环的一切。我曾尝试删除信息的一些方法,但它简化版,像我的语法。我发现微软支持的一个例子: http://support.microsoft.com/kb/317666 但我想用这一点的同时,而不是为每个。

有任何意见或问题是AP preciated。

解决方案

 字符串nodeXPath =你的X路;

XmlDocument的文档=新的XmlDocument();
document.Load(/ *文件路径* /);

XmlNode的节点= document.SelectSingleNode(nodeXPath);
node.RemoveAll();

XmlNode的parentnode = node.ParentNode;
parentnode.RemoveChild(节点);
document.Save(文件路径);
 

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.

解决方案

string nodeXPath = "your x path";

XmlDocument document = new XmlDocument();
document.Load(/*your file path*/);

XmlNode node = document.SelectSingleNode(nodeXPath);
node.RemoveAll();

XmlNode parentnode = node.ParentNode;
parentnode.RemoveChild(node);
document.Save("File Path");

这篇关于删除XML使用选定XPath和一个for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆