473,246 Members | 1,534 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,246 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 1639
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
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
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
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.