473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1397
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 aot, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: =?ISO-8859-1?Q?S=F8ren_Reinke?= | last post by:
Hi there. I am trying to get to know LINQ in combination with a bindingsource and a datagridview. In the framework we have at work we have created a usercontrol to do filtering via...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
2
by: timor.super | last post by:
Hi group, In my database, I have a table with fields like this : id | title | xml ------------------------------------ 1 | title1 | <datas><data><item...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
1
by: silpa | last post by:
Hi, Suppose if there are 10 records in employee table. I have a stored procedure named GetEmpCount which returns employee count. (i.e., The answer is 10). i.e., Select count(*) from...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
4
by: =?Utf-8?B?SmFtZXMgUGFnZQ==?= | last post by:
Hi all another LINQ question!! to retrieve and display sql varbinary images I currently use the following code: Imports System.Data.SqlClient Imports System.Drawing Imports...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.