C# remove nodes listed into XmlNodeList from XML file



it's my first post here ^^,


I have a problem with my XML file.



<?xml version="1.0" encoding="UTF-8"?>
<config>
<settings>
<excelFilePath>C:\Temp\</excelFilePath>
<mailHost>smtp</mailHost>
<mailPort>25</mailPort>
<mailFrom>me@test.com</mailFrom>
<keepInCopy>
<mailCC>email1@test.com</mailCC>
<mailCC>email2@test.com</mailCC>
<mailCC>email3@test.com</mailCC>
</keepInCopy>
<mailSubject>My Mail subject</mailSubject>
<mailBodyPath>Templates\Template1.htm</mailBodyPath>
</settings>
</config>


In practice i just need to delete all tags . I'm currently trying this C# code, but nothing happens:



XmlDocument xml = new XmlDocument();
xml.Load(FilePath);

XmlNodeList xnList = xml.GetElementsByTagName(xmlTag);
foreach (XmlNode xn in xnList)
{
xn.RemoveChild(xn.FirstChild);
xml.Save(FilePath);
}


Could you help me please?


No comments:

Post a Comment