ti*********@gmail.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.otherthing =collections of string with values {"ghi",
"jkl"}
Here is a sample query:
XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
var query =
from date in doc.Root.Elements("date")
select new
{
thing = (from value in
date.Element("thing").Elements("value")
select (string)value).ToList(),
otherthing = (from value in
date.Element("otherthing").Elements("value")
select (string)value).ToList()
};
foreach (var item in query)
{
Console.WriteLine("things: {0}; other things: {1}",
item.thing.Aggregate((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(@"..\..\XMLFile1.xml");
DateTime now = DateTime.Now;
var item =
(from date in doc.Root.Elements("date")
where DateTime.Parse(date.Attribute("start").Value) <
now &&
DateTime.Parse(date.Attribute("end").Value) now
select new
{
thing = (from value in
date.Element("thing").Elements("value")
select (string)value).ToList(),
otherthing = (from value in
date.Element("otherthing").Elements("value")
select (string)value).ToList()
}).FirstOrDefault();
Console.WriteLine("things: {0}; other things: {1}",
item.thing.Aggregate((a, b) =a + ", " + b),
item.otherthing.Aggregate((a, b) =a + ", " + b));
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/