Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Code sample for answering a comment on this StackOverflow answer:
- http://stackoverflow.com/questions/875136/how-to-remove-an-xmlnode-from-xmlnodelist/875159#875159
- */
- using System;
- using System.Xml;
- namespace ConsoleApplication1
- {
- class Program
- {
- private const string XmlData =
- @"<root><child id=""1"">content 1</child><child id =""2"" additionalStuff=""more stuff here"">content 2</child></root>";
- public static void Main()
- {
- var xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(XmlData);
- // select all nodes containing an 'additionalStuff' attribute
- XmlNodeList nodes = xmlDoc.SelectNodes("//child[@additionalStuff]");
- foreach (XmlNode node in nodes)
- {
- // remove the attribute
- node.Attributes.RemoveNamedItem("additionalStuff");
- }
- // xmlDoc.Save(fileName);
- // display the resulting xml
- Console.WriteLine(xmlDoc.OuterXml);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement