使用选定的 Xpath 和 for 循环删除 XML

发布于 2024-11-17 14:00:36 字数 531 浏览 1 评论 0原文

我目前有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ弥猫深巷。 2024-11-24 14:00:36

为什么不使用XDocument

var xmlText = "<Elements><Element1 /><Element2 /></Elements>";
var document = XDocument.Parse(xmlText);

var element = document.XPathSelectElement("Elements/Element1");
element.Remove();

var result = document.ToString();

结果将为

或者:

var document = XDocument.Load(fileName);

var element = document.XPathSelectElement("Elements/Element1");
element.Remove();

document.Savel(fileName);

[编辑] 对于 .NET 2,您可以使用 XmlDocument

XmlDocument document = new XmlDocument();
document.Load(fileName);

XmlNode node = document.SelectSingleNode("Elements/Element1");
node.ParentNode.RemoveChild(node);

document.Save(fileName);

[EDIT]

如果需要删除所有子元素和属性:

XmlNode node = document.SelectSingleNode("Elements");
node.RemoveAll();

如果您需要保留属性,但删除元素:

XmlNode node = document.SelectSingleNode("Elements");
foreach (XmlNode childNode in node.ChildNodes)
    node.RemoveChild(childNode);

Why not to use XDocument?

var xmlText = "<Elements><Element1 /><Element2 /></Elements>";
var document = XDocument.Parse(xmlText);

var element = document.XPathSelectElement("Elements/Element1");
element.Remove();

var result = document.ToString();

result will be <Elements><Element2 /></Elements>.

Or:

var document = XDocument.Load(fileName);

var element = document.XPathSelectElement("Elements/Element1");
element.Remove();

document.Savel(fileName);

[Edit] For .NET 2, you can use XmlDocument:

XmlDocument document = new XmlDocument();
document.Load(fileName);

XmlNode node = document.SelectSingleNode("Elements/Element1");
node.ParentNode.RemoveChild(node);

document.Save(fileName);

[EDIT]

If you need to remove all child elements and attributes:

XmlNode node = document.SelectSingleNode("Elements");
node.RemoveAll();

If you need to keep attributes, but delete elements:

XmlNode node = document.SelectSingleNode("Elements");
foreach (XmlNode childNode in node.ChildNodes)
    node.RemoveChild(childNode);
清音悠歌 2024-11-24 14:00:36
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");
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");
哽咽笑 2024-11-24 14:00:36

您可以使用XmlDocument

string nodeXPath = "your x path";

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

XmlNode node = document.SelectSingleNode(nodeXPath);

if (node.HasChildNodes)
{
    //note that you can use node.RemoveAll(); it will remove all child nodes, but it will also remove all node' attributes.

    for (int childNodeIndex = 0; childNodeIndex < node.ChildNodes.Count; childNodeIndex++)
    {
        node.RemoveChild(node.ChildNodes[childNodeIndex]);
    }
}

document.Save("your file path"));

You can use XmlDocument:

string nodeXPath = "your x path";

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

XmlNode node = document.SelectSingleNode(nodeXPath);

if (node.HasChildNodes)
{
    //note that you can use node.RemoveAll(); it will remove all child nodes, but it will also remove all node' attributes.

    for (int childNodeIndex = 0; childNodeIndex < node.ChildNodes.Count; childNodeIndex++)
    {
        node.RemoveChild(node.ChildNodes[childNodeIndex]);
    }
}

document.Save("your file path"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文