Linq To Xml request

T

timor.super

Hi all,

I tryed to create a linq2xml request, but I wasn't able to.

Can someone help me ?

Here's the xml

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

this values are loaded, because DateTime.Now is between 'start'
attribute and 'end' attribute

Thanks in advance for any help

Best regards
 
M

Martin Honnen

<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));
 
T

timor.super

Whaaouu
Great !!

A very big thank you, I hope I can do this a day too :)

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top