As a fan of generics I would create a helper class (based on Robert Jordan code):
public class XmlNodeListHelper
{
public static ListCopyNodeList(XmlNodeList list)
{
Listcopy = new List ();
foreach (XmlNode node in list)
copy.Add (node);
return copy;
}
public static void RemoveNodes(XmlNodeList list)
{
foreach (XmlNode node in CopyNodeList(list))
node.ParentNode.RemoveChild(node);
}
}
And so the code in question would become just:
XmlNodeListHelper.RemoveNodes(xDoc.SelectNodes("//comment()"));
When using the latest compilers it could be made into a Mono.Rocks helper like:
public class XmlNodeListHelper
{
public static ListCopyAsList(this XmlNodeList list)
{
Listcopy = new List ();
foreach (XmlNode node in list)
copy.Add (node);
return copy;
}
public static void RemoveNodes(this XmlNodeList list)
{
foreach (XmlNode node in list.CopyAsList())
node.ParentNode.RemoveChild(node);
}
}
And then usage would be even simpler
Doc.SelectNodes("//comment()").RemoveNodes();
No comments:
Post a Comment