473,466 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create 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

✓ answered by Monomachus

@veenna
Expand|Select|Wrap|Line Numbers
  1. xDocument.Descendants("MapInfo").Descendants("text").FirstOrDefault(el => el.Value == "item1").Parent.Remove();
  2.  

6 2993
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
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...
0
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....
3
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...
23
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...
1
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...
1
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...
2
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...
0
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...
1
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...
5
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.