问题描述
我必须与从XML文件中删除特定节点的问题。
I have a question related to removing specific nodes from xml file.
下面是我的XML的示例:
Here is my sample of XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<nodeA attribute="1">
<nodeB attribute="table">
<nodeC attribute="500"></nodeC>
<nodeC attribute="5"></nodeC>
</nodeB>
<nodeB attribute="3">
<nodeC attribute="4"></nodeC>
<nodeC attribute="5"></nodeC>
<nodeC attribute="5"></nodeC>
</nodeB>
<nodeB attribute="placeHolder">
<nodeB attribute="toRemove">
<nodeB attribute="glass"></nodeB>
<nodeE attribute="7"></nodeE>
<nodeB attribute="glass"></nodeB>
<nodeB attribute="glass"></nodeB>
</nodeB>
</nodeB>
<nodeB attribute="3">
<nodeC attribute="4"></nodeC>
<nodeC attribute="5"></nodeC>
<nodeC attribtue="5"></nodeC>
</nodeB>
<nodeB attribute="placeHolder">
<nodeB attribute="toRemove">
<nodeB attribute="glass"></nodeB>
<nodeE attribute="7"></nodeE>
<nodeB attribute="glass"></nodeB>
<nodeB attribute="glass"></nodeB>
</nodeB>
</nodeB>
</nodeA>
</root>
我想删除节点节点B =的文档,删除
不删除该节点的儿童。从那以后,我需要做同样的事情节点B的属性=占位符
。结果的一部分将看起来像:
I would like to remove node nodeB="toRemove"
without removing childrens of this node. After that I need to do same thing with nodeB attribute="placeHolder"
. Part of result would look like that:
<nodeB attribute="3">
<nodeC attribute="4"></nodeC>
<nodeC attribute="5"></nodeC>
<nodeC attribtue="5"></nodeC>
</nodeB>
<nodeB attribute="glass"></nodeB>
<nodeE attribute="7"></nodeE>
<nodeB attribute="glass"></nodeB>
<nodeB attribute="glass"></nodeB>
我一直想这样的代码才达到的:
I have been trying code like this to achive that:
XmlNodeList nodeList = doc.SelectNodes("//nodeB[@attribute=\"toRemove\"]");
foreach (XmlNode node in nodeList)
{
foreach (XmlNode child in node.ChildNodes)
{
node.ParentNode.AppendChild(child);
}
node.ParentNode.RemoveChild(node);
}
doc.Save(XmlFilePathSource);
我能够找到节点与期望的属性的文档,删除或占位符,但是我不能动的孩子这种由一个水平节点上。你能帮助我在这种情况下?它可以使用LINQ,的XDocument,XmlReader中解决方案,但我更喜欢使用XmlDocument的工作。
感谢您的帮助,您可以提供我提前
I am able to locate node with desired attribute toRemove or placeHolder, however I am not able to move children of this nodes up by one level. Could you help me in this case? It can be solution with Linq, XDocument, XmlReader but I prefer working with XmlDocument.Thank you for any help you could provide me in advance.
编辑:
在本情况下,我用稍微修改代码(保持顺序)查野人写道波纹管。一旦删除
In this case I have used slightly modified code(to preserve order) that Chuck Savage wrote bellow. Once to remove
<nodeB attribute="toRemove"> </nodeB>
,然后做同样的
and then do the same with
<nodeB attribute="placeHolder"></nodeB>
下面是稍微修改代码
XElement root = XElement.Load(XmlFilePathSource);
var removes = root.XPathSelectElements("//nodeB[@attribute=\"toRemove\"]");
foreach (XElement node in removes.ToArray())
{
node.Parent.AddAfterSelf(node.Elements());
node.Remove();
}
root.Save(XmlFilePathSource);
XSLT通过@MiMo提供的方法是非常有用的,以及在这种情况下。
xslt approach provided by @MiMo is very useful as well in this case.
推荐答案
使用LINQ到XML和你的XPath,
Using Linq-to-XML and your XPath,
XElement root = XElement.Load(XmlFilePathSource); // or .Parse(string)
var removes = root.XPathSelectElements("//nodeB[@attribute=\"toRemove\"]");
foreach (XElement node in removes.ToArray())
{
node.AddBeforeSelf(node.Elements());
node.Remove();
}
root.Save(XmlFilePathSource);
请注意:XPath是提供 System.Xml.XPath
Note: XPath is available in System.Xml.XPath
注2:您可以向/从XmlDocument的使用的的,因为你喜欢的XmlDocument。
Note2: You can convert to/from XmlDocument using these extensions since you prefer XmlDocument.
这篇关于除去没有孩子的节点的父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!