473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

LINQ to XML Question

77 New Member
I have the following XML structure from which I want to select/remove the "second" Element which has the Attribute "Name" whose value = "Test"
Expand|Select|Wrap|Line Numbers
  1. <Application>
  2. <first>
  3. <second Name="Test">
  4. </second>
  5. <second Name="Whatever">
  6. </second>
  7. </first>
  8. </Application>
  9.  
  10. var x = from c in xd.Element("Application").Element("first").Descendants()
  11.            where c.Element("Application").Element("first").Element("second").Attribute("Name").Value=="Test"
  12. select c;
  13.  
Anyway what I have above does not work, and I've tried a lot of different things to no avail. There is something important that I seem to be missing about LINQ to XML. I read that I should be able to use Descendants() anywhere (without having to specify the hierarchy in element.element type syntax), but I haven't gotten that to work with a where clause or condition.
Oct 14 '09 #1
1 2183
stoogots2
77 New Member
Resolved. Not sure why this didn't work the first time...

Here is the code to find the matching elements / remove them / and save the xml file.

Expand|Select|Wrap|Line Numbers
  1. XDocument xd = XDocument.Load(sXmlFile);
  2.                 var x = from c in xd.Descendants("second")
  3.                         where c.Attribute("Name").Value == "Test"
  4.                         select c;
  5.  
  6. // To remove all matching elements
  7. x.Remove();
  8.  
  9. // To Save after finished
  10. xd.Save(sXmlFile);
  11.  
  12.  
Oct 15 '09 #2

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

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
1
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. ...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
1
by: hrishy | last post by:
Hi Terry Oops i never realised the mistake i have commited I apologise (i thought changing the subject line would make a new thread) I apologise (I thought Python programmers were smart and...
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
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
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...
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,...
1
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...
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
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.