473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7257
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
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,...

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.