all groups > c# > april 2006 >
You're in the

c#

group:

Removing Node from XMLDocument


Removing Node from XMLDocument Stuart Shay
4/13/2006 10:04:58 PM
c#: 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>

RE: Removing Node from XMLDocument Sayed Ibrahim Hashimi
4/13/2006 10:27:02 PM
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]
Re: Removing Node from XMLDocument Nick Hounsome
4/14/2006 2:14:23 PM

[quoted text, click to view]

Unnecessarily innefficient.
Just stream copy an XmlReader to an XmlWriter except for the nodes that you
don't want.

Re: Removing Node from XMLDocument Martin Honnen
4/14/2006 2:59:12 PM


[quoted text, click to view]


[quoted text, click to view]


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
AddThis Social Bookmark Button