I want to remove xml node from file where in xml user selects path?
That code does not work. It not remove path4 node how can I do it? Please help me. Thank you.
<WindowEntries>
<WindowEntry>
<Name>tbwUnitOverview</Name>
<View>TRN_UNIT</View>
<LU>TrnUnit</LU>
<DefaultWindowText> windowtext</DefaultWindowText>
<Flags>0</Flags>
</WindowEntry>
<WindowEntry>
<Name>tbwBrandOverView</Name>
<View>TRN_BRAND</View>
<LU>TrnBrand</LU>
<DefaultWindowText />
<Flags>0</Flags>
</WindowEntry>
<WindowEntry>
<Name>tbwProductCategory</Name>
<View>TRN_PROD_CATEGORY</View>
<LU>TrnProdCategory</LU>
<DefaultWindowText />
<Flags>0</Flags>
</WindowEntry>
XmlTextReader reader = new XmlTextReader("component.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();
XmlNode currNode;
string path4 = treeView1.SelectedNode.FullPath.ToString();
currNode = doc.SelectSingleNode(path4);
开发者_JAVA百科 currNode.RemoveAll();
doc.Save("component.xml");
The problem is you are removing all child nodes and attributes of the node you are selecting from the document and not the actual node itsself.
See: XmlNode.RemoveAll Method
If you want to remove the actual node you will need to access the parent node (XmlNode.ParentNode Property) and then call the RemoveChild method (XmlNode.RemoveChild Method) passing in the node you wish to remove like so:
string path4 = treeView1.SelectedNode.FullPath.ToString();
XmlNode nodeToRemove = doc.SelectSingleNode(path4);
XmlNode parentNode = nodeToRemove.ParentNode;
parentNode.RemoveChild(nodeToRemove);
Hope this helps.
精彩评论