473,396 Members | 1,895 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 to XML question

hello,

i've got the following xml structure:

<catalog>
<books>
<item key="" value="" />
<item key="" value="" />
...
</books>
<cds>
<item key="" value="" />
<item key="" value="" />
...
</cds>
</catalog>

now i'd like to build a query which will select all <itemelements in
a given section (e.g. books). i don't manage to get this straight. as
well i can't figure out how to retrieve the count on a anonymous
type/collection returned from a linq query. my linq book is ordered and
on its way, but not here yet. :)

could anyone please shed some light? thanks in advance.

matthias
--

Jun 27 '08 #1
2 982
Matthias S. wrote:
i've got the following xml structure:

<catalog>
<books>
<item key="" value="" />
<item key="" value="" />
...
</books>
<cds>
<item key="" value="" />
<item key="" value="" />
...
</cds>
</catalog>

now i'd like to build a query which will select all <itemelements in
a given section (e.g. books).
XDocument doc = XDocument.Load(@"catalog.xml");
IEnumerable<XElementbookItems =
doc.Root.Element("books").Elements("item");
IEnumerable<XElementcdItems =
doc.Root.Element("cds").Elements("item");

That simply selects the 'item' XElement objects. If you want to populate
some anonymous type then do it like this:

XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");

var query =
from item in doc.Root.Element("books").Elements("item")
select new { key = (string)item.Attribute("key"), value
= (string)item.Attribute("value") };

foreach (var item in query)
{
Console.WriteLine("{0}: {1}.", item.key, item.value);
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
that helped. thank you a ton.
--

Martin Honnen wrote:
Matthias S. wrote:
i've got the following xml structure:

<catalog>
<books>
<item key="" value="" / <item key="" value="" />
...
</books>
<cds>
<item key="" value="" / <item key="" value="" />
...
</cds>
</catalog>
now i'd like to build a query which will select all <itemelements
in a given section (e.g. books).

XDocument doc = XDocument.Load(@"catalog.xml");
IEnumerable<XElementbookItems =
doc.Root.Element("books").Elements("item");
IEnumerable<XElementcdItems =
doc.Root.Element("cds").Elements("item");

That simply selects the 'item' XElement objects. If you want to
populate some anonymous type then do it like this:

XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");

var query =
from item in
doc.Root.Element("books").Elements("item") select new
{ key = (string)item.Attribute("key"), value =
(string)item.Attribute("value") };

foreach (var item in query)
{
Console.WriteLine("{0}: {1}.", item.key, item.value);
}
Jun 27 '08 #3

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

Similar topics

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...
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>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...

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.