Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple

R

Reece

Maybe I shouldn't be using XML for what I want. I want to store some
program data and retrieve it easily, iterating through elements grabbing
child elements easily and reading attributes along the way.

Let's say I want to use the following statement (and yes it works in C#):

XElement contactsParsedIn = XElement.Parse(
@"<contacts>
<contact>
<name>Patrick Hines</name>
<items>
<item number='1'>onep</item>
</items>
</contact>
<contact>
<name>Gretchen Rivas</name>
</contact>
<contact>
<name>Scott MacDonald</name>
<phone type='home'>925-555-0134</phone>
<phone type='mobile'>425-555-0177</phone>
<section sn='1'>
<items>
<item number='1'>1onem</item>
<item number='2'>1twom</item>
<item number='3'>1threem</item>
<item number='4'>1fourm</item>
<item number='5'>1fivem</item>
</items>
</section>
<section sn='2'>
<items>
<item number='1'>2onem</item>
<item number='222'>2twom</item>
<item number='333'>2threem</item>
<item number='444'>2fourm</item>
<item number='5'>2fivem</item>
</items>
</section>
</contact>
<contact>
<name>Patrick Smith</name>
</contact>
</contacts>");

And let's further say that I want to check out the info about Scott
MacDonald where I would like to find the items his "section" element where
the sn attribute = 2 and then I want to iterate through the items in that
section and get the number attribute for each item and the value of the
associated element.

So far I have seen no Linq to XML samples that handle this type of
situation. Certainly not in an elegant way.

Pseudo code for what I want might be like:

Get contact element where name =="scot McDonald"
Move to the section elements until sn attribute == 2
Foreach( item in items)
{
writeline("number attribute = " + item.attribute.number;
writeline("item value=" + item.element.value;
}

And the output would look like this:

number attribute=1
item value=2onem
number attribute=222
item value=2twom
number attribute=333
item value=2threem
number attribute=444
item value=2fourm
number attribute=5
item value=2fivem

I would appreciate any useful hints or ideas. Or if you can bang out the
general code or close to it, I would love to take that and run with it.

Thank you,

Reece
 
J

Jeroen Mostert

Reece said:
Maybe I shouldn't be using XML for what I want. I want to store some
program data and retrieve it easily, iterating through elements grabbing
child elements easily and reading attributes along the way.

Let's say I want to use the following statement (and yes it works in C#):

XElement contactsParsedIn = XElement.Parse(
@"<contacts>
<contact>
<name>Patrick Hines</name>
<items>
<item number='1'>onep</item>
</items>
</contact>
<contact>
<name>Gretchen Rivas</name>
</contact>
<contact>
<name>Scott MacDonald</name>
<phone type='home'>925-555-0134</phone>
<phone type='mobile'>425-555-0177</phone>
<section sn='1'>
<items>
<item number='1'>1onem</item>
<item number='2'>1twom</item>
<item number='3'>1threem</item>
<item number='4'>1fourm</item>
<item number='5'>1fivem</item>
</items>
</section>
<section sn='2'>
<items>
<item number='1'>2onem</item>
<item number='222'>2twom</item>
<item number='333'>2threem</item>
<item number='444'>2fourm</item>
<item number='5'>2fivem</item>
</items>
</section>
</contact>
<contact>
<name>Patrick Smith</name>
</contact>
</contacts>");

And let's further say that I want to check out the info about Scott
MacDonald where I would like to find the items his "section" element where
the sn attribute = 2 and then I want to iterate through the items in that
section and get the number attribute for each item and the value of the
associated element.

So far I have seen no Linq to XML samples that handle this type of
situation. Certainly not in an elegant way.

Pseudo code for what I want might be like:

Get contact element where name =="scot McDonald"
Move to the section elements until sn attribute == 2
Foreach( item in items)
{
writeline("number attribute = " + item.attribute.number;
writeline("item value=" + item.element.value;
}
Constructing "items" could look like this in LINQ to XML:

var items =
from contact in contactsParsedIn.Elements("contact")
where contact.Element("name").Value == "Scott MacDonald"
let section = contact.Elements("section").Where(s => (int)
s.Attribute("sn") == 2).First()
from item in section.Element("items").Elements("item")
select new { Number = (int) item.Attribute("number"), Value = item.Value };

I find this pretty straightforward, though it will be more elegant once LINQ
to XSD arrives and allows strong typing; in VB.NET, this can already be
written down more elegantly.

If you don't like LINQ to XML, XPath still works. Look at
XElement.XPathSelectElements(). I have no time to construct the
corresponding expression now, as I have to leave for work. :)
 
R

Reece

Thank you, Jeroen. It compiled and now I will study it and digest it.

Thanks again,

Reece
 
R

Reece

I now believe in Linq to XML. I had looked at the main page you link to and
several other web pages before my original post, but you brought it all
together for me with your query and the link to the 101 LINQ samples which
allowed me to put together the following statement that works with your
query:

foreach (var myitem in items)
Console.WriteLine(myitem.Number + " " + myitem.Value);

You helped me cross the bridge. I just needed to see it all working
together. Big thanks again!

Reece.
 
Top