Re: LINQ XML Determine Parent Node
- From: "Stuart Shay" <sshay@xxxxxxxxx>
- Date: Tue, 15 Dec 2009 21:42:56 -0500
Pete:
Thanks for your help, Got it to work
SPS
IEnumerable<ActivityReference> activityRefs =
from parent in root
.Descendants(ns1 + "Folders")
.Descendants(ns1 + "History")
from acfRef in parent
.Descendants(ns1 + "ActivityRef")
select new ActivityReference
{
ActivityDate = DateTime.Parse(acfRef.Element(ns1 + "Id").Value),
FolderName = acfRef.Parent.Name.LocalName,
Sport = acfRef.Parent.Attribute("Name").Value,
};
"Peter Duniho" <no.peted.spam@xxxxxxxxxxxxxxxxxx> wrote in message news:uwMLl7efKHA.6000@xxxxxxxxxxxxxxxxxxxxxxx
SPS101 wrote:Hello All
I have the following XML, I want to get a list of all the Activity References Id and there parents which can be Running, Biking, Other, MultiSport or a custom extension that I can define.
What is the best way to get the Parent Name and Attritubte in my code below,
Why not just capture the parent before descending into the descendants? Off the top of my head (i.e. this exact syntax may or may not work :) )...
IEnumerable<ActivityReference> activityRefs =
from parent in root
.Descendants(ns1 + "Folders")
.Descendants(ns1 + "History")
from actRef in parent
.Descendants(ns1 + "ActivityRef")
select new ActivityReference
{
Id = DateTime.Parse(actRef.Element(ns1 + "Id").Value),
Parent = parent.Name,
ParentName = parent.Attribute("Name").Value,
};
Alternatively, you can just use "actRef.Parent" to retrieve the parent element of the "actRef" element. I suspect there's no significant performance advantage of the other method over doing it that way, and it's arguably simpler to just use the Parent property instead (especially if I messed up the syntax above and there's more code you need to write to get it to work :) ).
Pete
.
- Follow-Ups:
- Re: LINQ XML Determine Parent Node
- From: Peter Duniho
- Re: LINQ XML Determine Parent Node
- References:
- LINQ XML Determine Parent Node
- From: SPS101
- Re: LINQ XML Determine Parent Node
- From: Peter Duniho
- LINQ XML Determine Parent Node
- Prev by Date: Re: General Question on Structure
- Next by Date: Re: LINQ XML Determine Parent Node
- Previous by thread: Re: LINQ XML Determine Parent Node
- Next by thread: Re: LINQ XML Determine Parent Node
- Index(es):
Relevant Pages
|