469,306 Members | 2,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Delete element from xml file, based on sub-element

65
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 2807
Dormilich
8,651 Expert Mod 8TB
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 Expert 100+
@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 Expert 100+
@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 Expert 4TB
I don't understand. That's not XML or XSL or XPath or anything I've seen.
May 10 '10 #5
Monomachus
127 Expert 100+
@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 Expert 4TB
Ok. Didn't know this was a Windows only question.
May 10 '10 #7

Post your reply

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

Similar topics

2 posts views Thread by Ryan | last post: by
23 posts views Thread by da Vinci | last post: by
1 post views Thread by zhiwei wang | last post: by
1 post views Thread by Matt Hamilton | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.