Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

LINQ to XML question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matthias S. - 16 Apr 2008 16:46 GMT
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 <item> elements 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
--
Martin Honnen - 16 Apr 2008 17:08 GMT
> i've got the following xml structure:
>
[quoted text clipped - 13 lines]
> now i'd like to build a query which will select all <item> elements in
> a given section (e.g. books).

            XDocument doc = XDocument.Load(@"catalog.xml");
            IEnumerable<XElement> bookItems =
doc.Root.Element("books").Elements("item");
            IEnumerable<XElement> cdItems =
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);
            }
Signature


    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Matthias S. - 16 Apr 2008 18:34 GMT
that helped. thank you a ton.
Signature


> > i've got the following xml structure:
> >
[quoted text clipped - 32 lines]
>                 Console.WriteLine("{0}: {1}.", item.key, item.value);
>             }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.