473,788 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 999
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<XEl ementbookItems =
doc.Root.Elemen t("books").Elem ents("item");
IEnumerable<XEl ementcdItems =
doc.Root.Elemen t("cds").Elemen ts("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( @"..\..\XMLFile 1.xml");

var query =
from item in doc.Root.Elemen t("books").Elem ents("item")
select new { key = (string)item.At tribute("key"), value
= (string)item.At tribute("value" ) };

foreach (var item in query)
{
Console.WriteLi ne("{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<XEl ementbookItems =
doc.Root.Elemen t("books").Elem ents("item");
IEnumerable<XEl ementcdItems =
doc.Root.Elemen t("cds").Elemen ts("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( @"..\..\XMLFile 1.xml");

var query =
from item in
doc.Root.Elemen t("books").Elem ents("item") select new
{ key = (string)item.At tribute("key"), value =
(string)item.At tribute("value" ) };

foreach (var item in query)
{
Console.WriteLi ne("{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
16423
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 write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
4
549
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.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
1
1867
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. Thanks, Miguel
9
2505
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
4
5099
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>
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.