473,554 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete element from xml file, based on sub-element

65 New Member
hi all,


I have this XML file

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Settings>
  3.   <MapInfo>
  4.     <text>item1</text>
  5.     <positionLeft>55.426666259765625</positionLeft>
  6.     <positionTop>163.94999694824219</positionTop>
  7.     <original>item1</original>
  8.   </MapInfo>
  9.   <MapInfo>
  10.     <text>item2</text>
  11.     <positionLeft>103.42666625976563</positionLeft>
  12.     <positionTop>110.94999694824219</positionTop>
  13.     <original>name</original>
  14.   </MapInfo>
  15. </Settings>
  16.  
How do i remove mapinfo node with all its sub nodes based on 'text' element it has?

I tried something like this

Expand|Select|Wrap|Line Numbers
  1. xDocument.Descendants().Descendants("MapInfo").Descendants().Where(e => e.Value == "item1").Remove();
  2.  
it just removed text node from mapinfo.

Any idea?


Regards
Veena
May 5 '10 #1
6 2995
Dormilich
8,658 Recognized Expert Moderator Expert
because you call .Remove() on that text node. you have to climb up to <MapInfo> and call .Remove() there.
May 5 '10 #2
Monomachus
127 Recognized Expert New Member
@veenna
Expand|Select|Wrap|Line Numbers
  1. xDocument.Descendants("MapInfo").Descendants("text").FirstOrDefault(el => el.Value == "item1").Parent.Remove();
  2.  
May 5 '10 #3
Monomachus
127 Recognized Expert New Member
@Monomachus
What I made there is selecting first the element <text>item1</text> and than selecting his parent, which obviously is MapInfo, and remove it.
May 6 '10 #4
drhowarddrfine
7,435 Recognized Expert Expert
I don't understand. That's not XML or XSL or XPath or anything I've seen.
May 10 '10 #5
Monomachus
127 Recognized Expert New Member
@drhowarddrfine
That's LINQ to XML. It is a .NET thing. Let's say it's a "framework" to deal with different kind-of lists. It is used with Collections, SQL and XML in .NET.

More information here - LINQ to XML
May 10 '10 #6
drhowarddrfine
7,435 Recognized Expert Expert
Ok. Didn't know this was a Windows only question.
May 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
8793
by: Ryan | last post by:
I have a table in my database on SQL Server which holds a file name that refers to a file that is stored on the server. I would like to create a trigger to delete this file from the server if the row in the table is deleted. I have been trying to use this command in a trigger (<filename> is the name and path of the file): xp_cmdshell...
0
2083
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath,...
3
1483
by: Huahe | last post by:
I try to delete a file in a for each loop. My code checks if the file exists and if it does, it will delete the file and create a new file with the same name. The first time it works perfect, but the second time it gives me a FileIOException. I want to prevent this from happening. What can i do to make sure the file isn't in use anymore the...
23
8916
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use the remove function that is included with <cstdio> but cannot get it to work properly. My reference book has the following....
1
1946
by: zhiwei wang | last post by:
I am new to dealing with the file system using C. And couldnt find anyone in my office to help me out. :( okay here is my questions. If I want to delete a file in a directory other than my working directory, I would construct a string look like this: str="../../foo/bar/foobar" add use "remove(str);" to delete this file. A lot of my work...
1
1877
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I am not able to delete the file because "The process cannot access the file ... It is being used by another process". I also get this error when trying to delete through explorer on the server. I...
2
4735
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple file sharing. And have specified the "Allow network users to change my files." option as well. Then on my laptop (xp home sp2) I have mapped a...
0
2832
by: smanisankar | last post by:
hi, the following is the full page code for uploading a file to server. since i got no idea to overwrite the file, i want delete the file if the file is already uploaded. i got the folder name and filename of the file to delete from the request.QueryString("path") so i got the above error when i try to delete the file before upload. Please...
1
2263
by: wizardRahl | last post by:
Hello, I have a problem with a block of code I'm trying to use that enables me to delete a file from not only the database, but the referenced folder in which the file is located. It will delete the file from the database (removes the picture from the form), but doesn't permantely remove the file. Here's what I have: Private Sub...
5
1920
by: agarwasa2008 | last post by:
Hi, I would like to delete a record based on a user entered string. Here are the details. I have a txtFind textbox. A string is entered by the user. Based on that string value it displays that one record in the fields in the frmDeleteComponent form. There is also a lstDelete listBox which displays all the record. But for some reason it...
0
7808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7893
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...
1
5441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5162
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...
0
3570
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...
0
3556
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2022
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

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.