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
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