473,386 Members | 1,873 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,386 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 1398
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 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.