473,480 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

LINQtoXML

I'm running the following code.

XElement r = XElement.Load("info.xml");
IEnumerable<XElement>
des = r.Descendants("A").Descendants("A1");

foreach (XElement d in des)
string v = d.Descendants("A1a")
.First<XElement>().Value;

I was expecting LINQ to give me more easy
access to the data structure. Perhaps
someone can suggest some changes.

1. Can i shorten the assignment to des
somehow? Something like "A.A1" would be
very helpful.

2. Why do i need to specify the type to
First() using <XElement>? Isn't it always
a XElement? How can i retrieve an int or
double instead of string?

3. Frankly, i was expecting LINQ to give
me a structure where intellisense would
let me "dot my way through" to the data
i need. Is that doable?

Also, after some reading, i'm still
unclear about the relation between
XElement and XNode (and the other X's as
well, but that's the two that _sound_
like a related leaf-node pair).

K Viltersten


Jun 27 '08 #1
5 1645
On Jun 5, 1:57 pm, "K Viltersten" <t...@viltersten.comwrote:
I'm running the following code.

XElement r = XElement.Load("info.xml");
IEnumerable<XElement>
des = r.Descendants("A").Descendants("A1");

foreach (XElement d in des)
string v = d.Descendants("A1a")
.First<XElement>().Value;

I was expecting LINQ to give me more easy
access to the data structure. Perhaps
someone can suggest some changes.

1. Can i shorten the assignment to des
somehow? Something like "A.A1" would be
very helpful.
You could use var :)

It sounds like you really want something XPath-like, in which case why
not use XPathSelectElements("A/A1")?
2. Why do i need to specify the type to
First() using <XElement>? Isn't it always
a XElement?
Yes, I'd expect so. What happens if you just call .First().Value? What
compiler error do you get?
How can i retrieve an int or double instead of string?
You'll need to use int.Parse etc. For XAttribute, there's an explicit
conversion which assumes the attribute value is in the appropriate XSD
format - I don't think there's anything similar for XElement.
3. Frankly, i was expecting LINQ to give
me a structure where intellisense would
let me "dot my way through" to the data
i need. Is that doable?
Given that the structure of your data depends on runtime information,
I don't see how that would be possible in a statically typed language.

It could be done in something like Boo, admittedly (with its bit of
duck typing).
Also, after some reading, i'm still
unclear about the relation between
XElement and XNode (and the other X's as
well, but that's the two that _sound_
like a related leaf-node pair).
XNode is just an (indirect) base class of XElement. (XElement derives
directly from XContainer, which derives directly from XNode.)

Jon
Jun 27 '08 #2
K Viltersten wrote:
2. Why do i need to specify the type to
First() using <XElement>? Isn't it always
a XElement? How can i retrieve an int or
double instead of string?
You can cast XElement or XAttribute to double or int or other types:
double n = (double)d.Descendants("A1a").FirstOrDefault();

See
http://msdn.microsoft.com/en-us/libr..._explicit.aspx
for the types to which you can cast.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #3
On Jun 5, 2:28 pm, Martin Honnen <mahotr...@yahoo.dewrote:
K Viltersten wrote:
2. Why do i need to specify the type to
First() using <XElement>? Isn't it always
a XElement? How can i retrieve an int or
double instead of string?

You can cast XElement or XAttribute to double or int or other types:
double n = (double)d.Descendants("A1a").FirstOrDefault();
Doh! Thanks for the correction. I'd seen that the Value property was
just a string - didn't check for explicit casts.

<slaps forehead>

Jon
Jun 27 '08 #4
K Viltersten wrote:
I'm running the following code.

XElement r = XElement.Load("info.xml");
IEnumerable<XElement>
des = r.Descendants("A").Descendants("A1");

foreach (XElement d in des)
string v = d.Descendants("A1a")
.First<XElement>().Value;

I was expecting LINQ to give me more easy
access to the data structure. Perhaps
someone can suggest some changes.
if you only care about getting the Elements that you want at any depth
and don't need the Hierarchy context, then you can simply have done...

XElement info = XElement.Load("info.xml");
IEnumerable<intA1IntValues = from x in info.DecendantsAndSelf("A1a")
Select (int)x;

I'm sure you would agree, that's pretty elegant.

Cheers Tim.

--

Jun 27 '08 #5
>I'm running the following code.
>>
XElement r = XElement.Load("info.xml");
IEnumerable<XElement>
des = r.Descendants("A").Descendants("A1");

foreach (XElement d in des)
string v = d.Descendants("A1a")
.First<XElement>().Value;

I was expecting LINQ to give me more easy
access to the data structure. Perhaps
someone can suggest some changes.

if you only care about getting the Elements that you want at any depth
and don't need the Hierarchy context, then you can simply have done...

XElement info = XElement.Load("info.xml");
IEnumerable<intA1IntValues = from x in info.DecendantsAndSelf("A1a")
Select (int)x;

I'm sure you would agree, that's pretty elegant.

Yes, it is. However, i do care about the
hierarchy context in this case. Thanks,
everybody, for the answers!

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
952
by: Andy B | last post by:
Ok, I have seen linq mentioned before as well as seeing it online and in vs2008 a few times here and there. What is it, what is it useful for, and are there any good walkthroughs/tutorials on using...
2
4878
by: David Thielen | last post by:
Hi; I have a small XML file that I need to read/change from my app. Is there some easy way to map from XML to my objects so I can just read it in to my objects, change the objects as needed,...
0
6920
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...
1
6758
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...
0
7010
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
4799
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
4499
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
3011
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
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
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...

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.