473,379 Members | 1,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Removing Node from XMLDocument

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>
Apr 14 '06 #1
3 2986
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
"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>

Apr 14 '06 #2


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

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 14 '06 #3

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ug**************@TK2MSFTNGP04.phx.gbl...


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


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rene.rugerio[at]gmail.com | last post by:
hi forum dumb question here i have this xmldocument which is from a certain xml looping in the nodes i can see which if the attributes are empty the thing here is that i just dont know how to...
2
by: GhislainTanguay | last post by:
This is my XML file and below you will find my code to remove it... I was thinking that it's a simple task but this code doesn't work. Anybody have a better solution? <Advertisements>
3
by: e-mid | last post by:
Here is an xml structure. i want to remove <a> nodes that do not have any child. How can i do that in csharp? <root> <a> <b/> </a> <a/> <a/> <a> <c/>
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
2
by: Keith M | last post by:
Hi, I have found a very useful piece of code in the msdn which shows how to load an xml config file, search for a particular value and replace it. Could someone possibly convert it to C# from...
11
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
3
by: GhislainTanguay | last post by:
This is my XML file and below you will find my code to remove it... I was thinking that it's a simple task but this code doesn't work. Anybody have a better solution? <Advertisements>
1
by: John Wilhelm | last post by:
I'm having a problem in by VB.net 2005 application. When i try to get a node from my app.config file the node come back with "nothing". The xmldocment loads OK, but I can't retrive a node. The...
1
by: Andrus | last post by:
How to remove whole Xmlnode so that outer tags are also removed ? To reproduce, run the code. Observed result: <Query> <DataSourceName>DS1</DataSourceName> <QueryParameters>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.