471,338 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Linq To Xml request

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
Aug 22 '08 #1
2 1341
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/
Aug 23 '08 #2
Whaaouu
Great !!

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

Best regards

On 23 août, 13:43, Martin Honnen <mahotr...@yahoo.dewrote:
timor.su...@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/
Aug 23 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by =?ISO-8859-1?Q?S=F8ren_Reinke?= | last post: by
9 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
2 posts views Thread by timor.super | last post: by
reply views Thread by =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post: by
4 posts views Thread by =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post: by
14 posts views Thread by thj | last post: by
4 posts views Thread by =?Utf-8?B?SmFtZXMgUGFnZQ==?= | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.