Using LINQ to Query XML Documents C# Help

Now that you can get your XML documents into an XDocument object and work with the various parts of this document, you can also use LINQ to XML to query your XML documents and work with the results.

Querying Static XML Documents

You will notice that querying a static XML document using LINQ to XML takes almost no work at all, The following example makes use of the hamlet. xml file and queries to get all the players (actors) that appear in the play, Each one of these players is defined in the XML document with the <PERSONAL> element:

In this case, an XDocument object loads up a physical XMLfile (hamlet .xml) and then performs a LlNQ query over the contents of the document: var query = from people in xdoc.Descendants(“PERSONAL”) select people. Value;
The people object is an object that is a representation of all the <PERSONAL>elements found in the document. Then the select statement gets at the values of these elements. From there, a Console WriteLine () method is used to write out a count of aU the players found using query. Count ( ).

Then, each of the items is written to the screen in a foreach loop. The results you should see are presented here:
26 Players Found
CLAUDIUS,king of Denmark.
HAMLET,son to the late king, and nephew to the present king.
POLONIUS, lord chamberlain.
HORATIO,friend to Hamlet.
LAERTES, son to Polonius.
LUCIANUS,nephew to the king.

VOLTIMAND
CORNELIUS
ROSENCRANTZ
GUILDENSTERN
OSRIC
A Gentleman
A Priest.
MARCELLUS
BERNARDO
FRANCISCO, a soldier.
REYNALDO, servant to Polonius.
Players.
Two Clowns, grave-diggers.
FORTINBRAS, prince of Norway.
A Captain.
English Ambassadors.
GERTRUDE, queen of Denmark, and mother to Hamlet.
OPHELIA, daughter to Polonius.
Lords, Ladies, Officers, Soldiers, Sailors, Messengers, and other Attendants.
Ghost of Hamlet’s Father.

Querying Dynamic XML Documents

A lot of dynamic XML documents are available on the Internet these days. You will find blog feeds, podcast feeds, and more that provide an XML document by sending a request to a specific URL endpoint. These feeds can be viewed either in the browser, through an RSS-aggregator, or as pure XML.

Looking at thiscode, you can see thatthe Load () method offof the XDocument objectpoints to a URL where the XML isretrieved.The firstquery pulls out allthe main sub-elements of the <cacnel> element in the feed and createsnew objects called Title, Description. and Link to get at the values of these sub-elements, From there,a foreach statement is run to iterate through all the items found in this query, The results are as follows:

TITLE: Bill Even’S Blog
DESCRIPTION: Code. Life and Community
LINK: http://geekswithblogs.net/evjen/Default.aspx

The second query works through allthe <i tern>elements and the various sub-elements ithas (these are all the blog entries found in the blog).Though a lotof the items found are rolledup intoproperties,in the foreach loop,only the TitIe property isused, You will see something similar to the following results from this query:

AJAX Control Toolkit Controls Grayed OUt – HOW TO FIX
Welcome .NET 3.5!
Visual Studio 2008 Released
lIS 7.0 Rocks the House!
Word Issue – Couldn’t Select Text
Microsoft Releases XML Schema Designer CTPI
Silverlight Book
Microsoft ~fiti as a beta
ReSharper on Visual Studio 2008
Windows Vista Updates for Performance and Reliability Issues
New Version of ODP.NET for .NET 2.0 Released as Beta Today
First Review of Professional XML
Go to MIX07 for free!

Microsoft Surface and the Future of HomeComputing?
Alas my friends – I’m *not* TechEd bound
NewBook – Professional VB2005 with .NET 3.0!
An article showing Oracle and .NETworking together
MyLate’st Book – Professional XML
CISCOVPNClient Software on Windows Vista
Server-Side Excel Generation
Scott Guthrie Gives Short Review of Professional ASP.NET2.0 SE
Windows Forms Additions in the Next Version of .NET
Tag. I’m It.

Posted on October 31, 2015 in LINQ to XML

Share the Story

Back to Top