ti*********@gma il.com wrote:
<datas>
<date start="12/07/2008" end="31/08/2008">
<thing>
<value>abc</value>
<value>def</value>
</thing>
<otherthing>
<value>ghi</value>
<value>jkl</value>
</otherthing>
</date>
<date start="01/09/2008" end="31/08/2009">
<thing>
<value>mno</value>
<value>pqr</value>
</thing>
<otherthing>
<value>stu</value>
<value>vwx</value>
</otherthing>
</date>
</datas>
I want to have an object that looks like this
myObject.thing =collections of string with values {"abc", "def"}
myObject.othert hing =collections of string with values {"ghi",
"jkl"}
Here is a sample query:
XDocument doc = XDocument.Load( @"..\..\XMLFile 1.xml");
var query =
from date in doc.Root.Elemen ts("date")
select new
{
thing = (from value in
date.Element("t hing").Elements ("value")
select (string)value). ToList(),
otherthing = (from value in
date.Element("o therthing").Ele ments("value")
select (string)value). ToList()
};
foreach (var item in query)
{
Console.WriteLi ne("things: {0}; other things: {1}",
item.thing.Aggr egate((a, b) =a + ", " + b),
item.otherthing .Aggregate((a, b) =a + ", " + b));
}
Output is
things: abc, def; other things: ghi, jkl
things: mno, pqr; other things: stu, vwx
If you want to filter on the date and only output the first value then
found then the following might be what you are looking for:
XDocument doc = XDocument.Load( @"..\..\XMLFile 1.xml");
DateTime now = DateTime.Now;
var item =
(from date in doc.Root.Elemen ts("date")
where DateTime.Parse( date.Attribute( "start").Va lue) <
now &&
DateTime.Parse( date.Attribute( "end").Valu e) now
select new
{
thing = (from value in
date.Element("t hing").Elements ("value")
select (string)value). ToList(),
otherthing = (from value in
date.Element("o therthing").Ele ments("value")
select (string)value). ToList()
}).FirstOrDefau lt();
Console.WriteLi ne("things: {0}; other things: {1}",
item.thing.Aggr egate((a, b) =a + ", " + b),
item.otherthing .Aggregate((a, b) =a + ", " + b));
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/