473,614 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.othert hing =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 1411
ti*********@gma il.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.othert hing =collections of string with values {"ghi",
"jkl"}
Here is a sample query:

XDocument doc = XDocument.Load( @"..\..\XMLFile 1.xml");
var query =
from date in doc.Root.Elemen ts("date")
select new
{
thing = (from value in
date.Element("t hing").Elements ("value")
select (string)value). ToList(),
otherthing = (from value in
date.Element("o therthing").Ele ments("value")
select (string)value). ToList()
};
foreach (var item in query)
{
Console.WriteLi ne("things: {0}; other things: {1}",
item.thing.Aggr egate((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( @"..\..\XMLFile 1.xml");

DateTime now = DateTime.Now;
var item =
(from date in doc.Root.Elemen ts("date")
where DateTime.Parse( date.Attribute( "start").Va lue) <
now &&
DateTime.Parse( date.Attribute( "end").Valu e) now
select new
{
thing = (from value in
date.Element("t hing").Elements ("value")
select (string)value). ToList(),
otherthing = (from value in
date.Element("o therthing").Ele ments("value")
select (string)value). ToList()
}).FirstOrDefau lt();
Console.WriteLi ne("things: {0}; other things: {1}",
item.thing.Aggr egate((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...@yaho o.dewrote:
timor.su...@gma il.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.othert hing =collections of string with values {"ghi",
"jkl"}

Here is a sample query:

* * * * * * *XDocument doc = XDocument.Load( @"..\..\XMLFile 1.xml");
* * * * * * *var query =
* * * * * * * * *from date in doc.Root.Elemen ts("date")
* * * * * * * * *select new
* * * * * * * * *{
* * * * * * * * * * *thing = (from value in
date.Element("t hing").Elements ("value")
* * * * * * * * * * * * * * * select (string)value). ToList(),
* * * * * * * * * * *otherthing = (from value in
date.Element("o therthing").Ele ments("value")
* * * * * * * * * * * * * * * * * *select (string)value). ToList()
* * * * * * * * *};
* * * * * * *foreach (var item in query)
* * * * * * *{
* * * * * * * * *Console.WriteL ine("things: {0}; other things: {1}",
item.thing.Aggr egate((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( @"..\..\XMLFile 1.xml");

* * * * * * *DateTime now = DateTime.Now;
* * * * * * *var item =
* * * * * * * * *(from date in doc.Root.Elemen ts("date")
* * * * * * * * *where DateTime.Parse( date.Attribute( "start").Va lue) <
now &&
* * * * * * * * * * * *DateTime.Parse (date.Attribute ("end").Valu e) now
* * * * * * * * *select new
* * * * * * * * *{
* * * * * * * * * * *thing = (from value in
date.Element("t hing").Elements ("value")
* * * * * * * * * * * * * * * select (string)value). ToList(),
* * * * * * * * * * *otherthing = (from value in
date.Element("o therthing").Ele ments("value")
* * * * * * * * * * * * * * * * * *select (string)value). ToList()
* * * * * * * * *}).FirstOrDefa ult();
* * * * * * *Console.WriteL ine("things: {0}; other things: {1}",
item.thing.Aggr egate((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
1810
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 textboxes on a datagridview. Therefore we databind through a bindingsource. But when i load the data from the database via LINQ instead of through a
9
2497
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
2
4469
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 name="item1">value1</ item><item name="item2">value2</item></data></datas> ......
0
2355
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 occuring enough to be a real cause for concern. The errors occur a couple times a week and the website is hit with constant traffic 24x7. Below are the two errors that are encountered. I had read something about MARS causing errors like this but...
1
3152
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 employee -------------------------------------------------------------------------------------------------------------------- I have drag and dropped GetEmpCount stored procedure on to the LINQ dbml designer. Now I am writing a method like as...
4
2178
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 not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
14
3670
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
1390
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 System.Drawing.Imaging Imports System.IO
3
12424
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
0
8197
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.