問題描述
我有一個(gè)關(guān)于從 xml 文件中刪除特定節(jié)點(diǎn)的問題.
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>
我想刪除節(jié)點(diǎn) nodeB="toRemove"
而不刪除該節(jié)點(diǎn)的子節(jié)點(diǎn).之后我需要對 nodeB attribute="placeHolder"
做同樣的事情.部分結(jié)果如下所示:
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>
我一直在嘗試這樣的代碼來實(shí)現(xiàn):
I have been trying code like this to achive that:
XmlNodeList nodeList = doc.SelectNodes("http://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);
我能夠找到具有所需屬性 toRemove 或 placeHolder 的節(jié)點(diǎn),但是我無法將此節(jié)點(diǎn)的子節(jié)點(diǎn)上移一級.在這種情況下你能幫我嗎?它可以是 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.
在這種情況下,我使用了 Chuck Savage 在下面編寫的稍微修改過的代碼(以保持順序).一次刪除
In this case I have used slightly modified code(to preserve order) that Chuck Savage wrote bellow. Once to remove
<nodeB attribute="toRemove"> </nodeB>
然后對
<nodeB attribute="placeHolder"></nodeB>
這里是稍微修改的代碼
XElement root = XElement.Load(XmlFilePathSource);
var removes = root.XPathSelectElements("http://nodeB[@attribute="toRemove"]");
foreach (XElement node in removes.ToArray())
{
node.Parent.AddAfterSelf(node.Elements());
node.Remove();
}
root.Save(XmlFilePathSource);
@MiMo 提供的 xslt 方法在這種情況下也非常有用.
xslt approach provided by @MiMo is very useful as well in this case.
推薦答案
使用 Linq-to-XML 和您的 XPath,
Using Linq-to-XML and your XPath,
XElement root = XElement.Load(XmlFilePathSource); // or .Parse(string)
var removes = root.XPathSelectElements("http://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:您可以使用 這些擴(kuò)展 與 XmlDocument 進(jìn)行轉(zhuǎn)換,因?yàn)槟矚g XmlDocument.
Note2: You can convert to/from XmlDocument using these extensions since you prefer XmlDocument.
這篇關(guān)于刪除沒有子節(jié)點(diǎn)的父節(jié)點(diǎn)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!