473,503 Members | 11,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regarding a query in LINQ to XML

33 New Member
Hi, Hope everybody is file here....

I have an xml like this
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Holidays>
  3.     <year year="2010">
  4.             <holiday day="03/01/2010" />
  5.             <holiday day="03/02/2010" />
  6.         </year>
  7.         <year year="2011">
  8.             <holiday day="03/01/2011" />
  9.             <holiday day="03/02/2011" />
  10.             <holiday day="03/03/2011" />
  11.         </year>
  12. </Holidays>
Now I want an string array of holidays for year 2010
I'm trying something like
Expand|Select|Wrap|Line Numbers
  1. XDocument holidays = XDocument.Load("path to xml/ xmlreader");
  2. var days = from el in holidays.Root.Elements("year")
  3.             where el.Attribute("year").Value == "2010"
  4.              select el.Elements("holiday");
  5. foreach (var date in days)
  6. {
  7. // But here date is not what i want like '03/01/2010' or '03/02/2010'
  8. }
  9.  
Can you please suggest how to extract the holidays for year 2010 from the xml.

Thanks :)

Edit: And also how do i do the same when the xml is of the form
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Holidays>
  3.   <Year Year="2010">
  4.     <Holiday>"03/01/2010"</Holiday>
  5.     <Holiday>"05/27/2010"</Holiday>
  6.   </Year>
  7.   <Year Year="2011">
  8.     <Holiday>"03/01/2011"</Holiday>
  9.     <Holiday>"05/27/2011"</Holiday>
  10.   </Year>
  11.  
  12. </Holidays>
  13.  
Aug 20 '10 #1
6 1479
Christian Binder
218 Recognized Expert New Member
date is of type XElement (holiday), you've to extract it's day-attribute and read it's value.
e.g.
date.Attributes("day").First().Value;

In your second example, the date is the value of your date-XElement, so you'd just have to do date.Value within you foreach-loop.
Aug 20 '10 #2
sanndeb
33 New Member
Thats ok to get the values using date.Attributes("day").First().Value inside the foreach loop... but what i wanted is to extract the dates directly using linq not inside another foreach loop.... i.e like at the end of the linq query i'll add a toList() method so that all day's will be listed in a list of string.
Aug 20 '10 #3
Christian Binder
218 Recognized Expert New Member
Sure you can do this
Expand|Select|Wrap|Line Numbers
  1. var days = from el in holidays.Root.Elements("year")
  2.            where el.Attribute("year").Value == "2010"
  3.            let holidayElem = el.Elements("holiday")
  4.            let attrib = holidayElem.Attributes("day").FirstOrDefault()
  5.            where attrib != null
  6.            select attrib.Value;
  7.  
Aug 20 '10 #4
sanndeb
33 New Member
But your query is returning only the first day of year 2010 i.e '03/01/2010', but the output should be of two days...'03/01/2010' & '03/02/2010'
Aug 20 '10 #5
Christian Binder
218 Recognized Expert New Member
Oh, I'm sorry.

Expand|Select|Wrap|Line Numbers
  1. var days = from el in holidays.Root.Elements("year")
  2.            where el.Attribute("year").Value == "2010"
  3.            from holidayElem in el.Elements("holiday")
  4.            let attrib = holidayElem.Attributes("day").FirstOrDefault()
  5.            where attrib != null
  6.            select attrib.Value;
  7.  
Aug 20 '10 #6
sanndeb
33 New Member
Yes thats it mate... getting the results perfectly... thanks a lot :)
Aug 20 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2081
by: LittleDBA | last post by:
Could anyone tell me what this query means? Thanks a lot. --------------------------------------- select username from dba_users where not exists (select 1 from dba_role_privs where username...
1
1330
by: kirang | last post by:
hi all, I am an M.Tech student of IITBombay,india. We are doing a project on access control in postgres. Is there any way in which, i can add a extra predicate to the sql query given to the...
2
1118
by: Grant Schenck | last post by:
Hello, I'm a Win32/Forms developer and have to do a little ASP. So, the requirement is that I need to build an ASP.NET solution. I built it using I guess standard ASP.NET in that get a...
0
1761
by: fiona | last post by:
Reading, Berkshire, UK 05 June 2007 - Crainiate Software make details available of the release of Objecto Framework 2.0, an upgrade to their enterprise business component framework, designed to...
11
12519
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
2
1000
manoj9849967222
by: manoj9849967222 | last post by:
Hi All I have a form which contains a command button"Command1" which is linked to a query. When i click the command button it executes the query. Now i want that the Query should close...
1
2214
by: 0to60 | last post by:
Let's say we have your basic Invoices and InvoiceItems table. If we load this in with LINQ: var query = from i in db.Invoices select i; When I then loop through my invoices, if I wanna...
1
10032
by: silpa | last post by:
Hi, I have an SQL query like this select distinct t1.prodID from Table1 t1 left join Table2 t2 on t2.prodID = t1.prodID left join Table3 t3 on t3.serialno = t2.Id and t3.Qty = 0 ...
4
1380
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...
0
7212
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
7098
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
7296
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,...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
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...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
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...
0
1524
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 ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.