473,785 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="EasyGP S 2.3" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="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 3023
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(Rem oveNodeEx.xml);

System.Console. WriteLine(doc.O uterXml);

XmlNodeList nodes = doc.SelectNodes (@"/gpx/wpt/extensions");
foreach (XmlNode node in nodes)
{
node.ParentNode .RemoveChild(no de);
}
System.Console. WriteLine("------------------");
System.Console. WriteLine(doc.O uterXml);
}
}

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="EasyGP S 2.3" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="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.Loa d(@"example.xml ");
XmlNamespaceMan ager namespaceManage r = new
XmlNamespaceMan ager(xmlDocumen t.NameTable);
namespaceManage r.AddNamespace( "gp",
"http://www.topografix. com/GPX/1/1");
XmlNodeList extensionsEleme nts = xmlDocument.Sel ectNodes(
"gp:gpx/gp:wpt/gp:extensions",
namespaceManage r
);
foreach (XmlNode node in extensionsEleme nts) {
node.ParentNode .RemoveChild(no de);
}
xmlDocument.Sav e(Console.Out); // could save to file too
--

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

"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:ug******** ******@TK2MSFTN GP04.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
1297
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 erase the ones aren't filled they appear like <node att1="" att2="yes" att3="ok" att4=""> i want to take att1 and att4 out, any ideas ?
2
4609
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
16040
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
10701
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 attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
2
7690
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 the VB I have? I also would like to use similar code to update the machine.config file so if someone could explain how to add a node ("appSettings") if node not found and also how to add a key value pair into the "appSettings" node if found I would...
11
4339
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
1222
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
2899
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 app.config file is listed below and the code is listed below that, the line with "-->>" is where i try to get the node. Can anyone see where I'm going wrong. A dump of the xmlDocument is listed below the code. Note: "MyKey" is set to "Reports"
1
3318
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> </QueryParameters>
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.