在XML文件中,例如:
<Snippets>
<Snippet name="abc">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
<Snippet name="def">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
</Snippets>
Run Code Online (Sandbox Code Playgroud)
当只给出属性名称(如abc
或def
)时,如何删除元素?
mar*_*c_s 17
你可以尝试这样的事情:
string xmlInput = @"<Snippets>
<Snippet name=""abc"">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
<Snippet name=""def"">
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
</Snippets>";
// create the XML, load the contents
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlInput);
// find a node - here the one with name='abc'
XmlNode node = doc.SelectSingleNode("/Snippets/Snippet[@name='abc']");
// if found....
if (node != null)
{
// get its parent node
XmlNode parent = node.ParentNode;
// remove the child node
parent.RemoveChild(node);
// verify the new XML structure
string newXML = doc.OuterXml;
// save to file or whatever....
doc.Save(@"C:\temp\new.xml");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35263 次 |
最近记录: |