472,986 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Linq > Get XML Node

Hello,

I am trying to get the node "album" in a XML file given its "id"
attribute:

<gallery>
<album
id = "1"
title="Intro"
....

So in this case I would like to get the album with id = "1" under
gallery. I am using the following:ml"));

XDocument file = XDocument.Load(path);

XElement node =
file.Document.Element("gallery").Elements("album")

I just don't know how to select the album node with ID = "1".

How to do this?

Thanks,
Miguel
Sep 7 '08 #1
2 7241
Well, I don't know XDocument very well, but it would be trivial in
XmlDocument:

XmlElement el = (XmlElement)file.SelectSingleNode("/gallery/
album[@id='1']");

Marc
Sep 7 '08 #2
shapper wrote:
I am trying to get the node "album" in a XML file given its "id"
attribute:

<gallery>
<album
id = "1"
title="Intro"
...

So in this case I would like to get the album with id = "1" under
gallery. I am using the following:ml"));

XDocument file = XDocument.Load(path);
Using XElement directly is usually simpler. XDocument is almost never required.
XElement node =
file.Document.Element("gallery").Elements("album")
file.Document is redundant; you can access the elements of the document
directly.
I just don't know how to select the album node with ID = "1".
Let's say "gallery" holds the <galleryelement, then

XElement album = (
from a in gallery.Elements("album")
where (int) a.Attribute("id") == 1
select a
).First();

should do it. This assumes that every album has an id and that there is an
album with id 1. A more robust version is

XElement album = (
from a in gallery.Elements("album")
where (int?) a.Attribute("id") == 1
select a
).FirstOrDefault();

This will return null if no such album exists, and it won't fail if there
are albums without ids.

You can rewrite these as simple expressions if you're so inclined:

XElement album = gallery.Elements("album").FirstOrDefault(a =(int?)
a.Attribute("id") == 1);

Some people prefer the query syntax even for simple queries.

--
J.
Sep 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
2
by: bissatch | last post by:
Hi, I am currently writing a simple PHP program that uses an XML file to output rows for a 'Whats New' page. Once written, I will only require updating the XML file and any pages that use the...
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...
0
by: Guzeppi | last post by:
Hi, i'm using linq to load an xml structure into my classes. the xml consists of the same node nested for multiple levels e.g. <node id="node_id01" name="node 01"> <node id="node_id0101"...
6
by: hon123456 | last post by:
Why does the compiler say that 'Linq' does not exist in the 'System' namespace when specifying a using statement as follows: using System.Linq; ----
0
by: Iaml | last post by:
I have an XML file and the depth of each node is unpredictable. I need to find the grandparent node which contains an element whose certain attribute has the given value. Then I will process...
1
by: Amil Hanish | last post by:
I had a VS 2005 web app (not using Linq yet). I migrated to VS 2008 and the app still worked fine. Now I want to try out Linq. I modified the web.config to load the Linq DLLs and added "using...
1
by: Author | last post by:
I have an xml document that looks *like* so: <Mammal name="Cat"> <Mammal name="African Tigers" > <Mammal name="Central African Tiger" /> <Mammal name="Ethiopian Tiger" /> </Mammal> <Mammal...
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.