2015-08-18 41 views
-6

我有一个文件下面的XML如何从文件中删除特定的XML标签,并将其保存回

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
     <!--Settings--> 
     <add key="url" value="http://vmcarekey.com"/> 
     <add key="user" value="admin"/> 
     <add key="pass" value="password"/> <!-- Remove this line --> 
    </appSettings> 
</configuration> 

我想与关键=“通行证”删除XML标签使用C#和保存原始文件中的xml。

我想输出XML看起来如下

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
     <!--Settings--> 
     <add key="url" value="http://vmcarekey.com"/> 
     <add key="user" value="admin"/> 
    </appSettings> 
</configuration> 

请指引我实现这一目标。 在此先感谢。

+0

的LINQ to XML - 阅读一些教程,这将是先导,为你。这项任务非常简单。 – MajkeloDev

+0

你是否研究过这个?这是你会发现很多信息的类型。您的问题会因为您没有显示任何研究工作而收到投票结果。 –

+0

它取决于你如何阅读你的XML,但你可以在那里找到答案: http://stackoverflow.com/questions/20611/removing-nodes-from-an-xmldocument – Froggiz

回答

2

它可以很容易地LinqToXml

var xDoc = XDocument.Load(filename); 
xDoc.XPathSelectElement("//appSettings/add[@key='pass']").Remove(); 
xDoc.Save(filename); 
1

做试试这个:

XDocument xdoc = XDocument.Load(filename); 
xdoc.Element("configuration").Element("appSettings").Elements("add") 
    .Where(x => (string)x.Attribute("key") == "pass").Remove(); 
xdoc.Save(filename); 
相关问题