Here is a solution
public class RemoveNodeEx
{
public static readonly string xml = @"
<gpx>
<wpt lat=""40.735517"" lon=""-74.028613"">
<time>2006-04-08T00:49:04Z</time>
<name>001</name>
<sym>Waypoint</sym>
<type>Other</type>
<extensions>
<label xmlns=""
http://www.topografix.com/GPX/gpx_overlay/0/3""> <label_text>001</label_text>
</label>
</extensions>
</wpt>
<wpt lat=""40.735517"" lon=""-74.028613"">
<time>2006-04-08T00:49:04Z</time>
<name>002</name>
<sym>Waypoint</sym>
<type>Other</type>
<extensions>
<label xmlns=""
http://www.topografix.com/GPX/gpx_overlay/0/3""> <label_text>002</label_text>
</label>
</extensions>
</wpt>
</gpx>
".Trim();
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(RemoveNodeEx.xml);
System.Console.WriteLine(doc.OuterXml);
XmlNodeList nodes = doc.SelectNodes(@"/gpx/wpt/extensions");
foreach (XmlNode node in nodes)
{
node.ParentNode.RemoveChild(node);
}
System.Console.WriteLine("------------------");
System.Console.WriteLine(doc.OuterXml);
}
}
Note: I had to remove the namespace declarations from the root element to
get this to work correctly.
Sayed Ibrahim Hashimi
www.sedodream.com [quoted text, click to view] "Stuart Shay" wrote:
> Hello All
>
> In the following XML Below which is a XMLDocument in my application.
>
> I want to remove the <extensions></extensions> Node completely.
>
> What is the best way to do this, my files are not that large
>
> Thanks
> Stuart
>
> <gpx xmlns="
http://www.topografix.com/GPX/1/1" version="1.1"
> creator="EasyGPS 2.3" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="
http://www.topografix.com/GPX/1/1 >
http://www.topografix.com/GPX/1/1/gpx.xsd >
http://www.topografix.com/GPX/gpx_overlay/0/3 >
http://www.topografix.com/GPX/gpx_overlay/0/3/gpx_overlay.xsd"> > <wpt lat="40.735517" lon="-74.028613">
> <time>2006-04-08T00:49:04Z</time>
> <name>001</name>
> <sym>Waypoint</sym>
> <type>Other</type>
> <extensions>
> <label xmlns="
http://www.topografix.com/GPX/gpx_overlay/0/3"> > <label_text>001</label_text>
> </label>
> </extensions>
> </wpt>
> <wpt lat="40.735517" lon="-74.028613">
> <time>2006-04-08T00:49:04Z</time>
> <name>002</name>
> <sym>Waypoint</sym>
> <type>Other</type>
> <extensions>
> <label xmlns="
http://www.topografix.com/GPX/gpx_overlay/0/3"> > <label_text>002</label_text>
> </label>
> </extensions>
> </wpt>
> </gpx>
>
>
[quoted text, click to view] Stuart Shay wrote:
[quoted text, click to view] > In the following XML Below which is a XMLDocument in my application.
>
> I want to remove the <extensions></extensions> Node completely.
>
> What is the best way to do this, my files are not that large
You can use the DOM XmlDocument, SelectNodes and RemoveChild as in
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"example.xml");
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable);
namespaceManager.AddNamespace("gp",
"
http://www.topografix.com/GPX/1/1");
XmlNodeList extensionsElements = xmlDocument.SelectNodes(
"gp:gpx/gp:wpt/gp:extensions",
namespaceManager
);
foreach (XmlNode node in extensionsElements) {
node.ParentNode.RemoveChild(node);
}
xmlDocument.Save(Console.Out); // could save to file too
--
Martin Honnen --- MVP XML