c# – 如何从文件中删除一个xml元素?

c# – 如何从文件中删除一个xml元素?,第1张

概述在一个 XML文件中,如: <Snippets> <Snippet name="abc"> <SnippetCode> code goes here </SnippetCode> </Snippet> <Snippet name="def"> <SnippetCode> code goes here </SnippetCode> </Snippet> 在一个 XML文件中,如:
<Snippets> <Snippet name="abc">   <SnippetCode>   code goes here   </SnippetCode> </Snippet> <Snippet name="def">   <SnippetCode>   code goes here   </SnippetCode> </Snippet></Snippets>

只有在给定属性名称(如abc或def)时,如何删除元素

解决方法 你可以尝试这样的东西:
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 contentsXmldocument 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");}
总结

以上是内存溢出为你收集整理的c# – 如何从文件中删除一个xml元素?全部内容,希望文章能够帮你解决c# – 如何从文件中删除一个xml元素?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1261283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存