471,350 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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 1591
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 discussion thread is closed

Replies have been disabled for this discussion.

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.