Removing Node from XMLDocument

S

Stuart Shay

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>
 
G

Guest

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
 
M

Martin Honnen

Stuart Shay wrote:

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
 
N

Nick Hounsome

Martin Honnen said:
Stuart Shay wrote:




You can use the DOM XmlDocument, SelectNodes and RemoveChild as in

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top