LINQ XML Determine Parent Node

S

SPS101

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,


<Folders>
<History>
<Running Name="Sample Running">
<ActivityRef>
<Id>2008-06-14T18:19:57Z</Id>
</ActivityRef>
</Running>
<Biking Name="Sample Biking">
<ActivityRef>
<Id>2008-07-04T19:46:07Z</Id>
</ActivityRef>
<ActivityRef>
<Id>2008-06-22T21:14:22Z</Id>
</ActivityRef>
<ActivityRef>
<Id>2008-06-15T22:24:13Z</Id>
</ActivityRef>
<ActivityRef>
<Id>2008-06-14T18:19:57Z</Id>
</ActivityRef>
</Biking>
</History>
<Folders>


IEnumerable<ActivityReference> activityRefs = from actRef in
root.Descendants(ns1 + "Folders").Descendants(ns1 +
"History").Descendants(ns1 + "ActivityRef")
select new ActivityReference
{
Id = DateTime.Parse(actRef.Element(ns1 + "Id").Value),
Parent = " Get Parent Node - Biking,Running Etc. ",
ParentName = " Get Parent Node Name Attribute = 'Running Sample'",
};
 
P

Peter Duniho

SPS101 said:
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
 
S

Stuart Shay

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,
};
 
P

Peter Duniho

Stuart said:
Pete:

Thanks for your help, Got it to work

I would not recommend using both the double "from parent" _and_ the
"acfRef.Parent" syntax. You should need only one or the other.

For example:

IEnumerable<ActivityReference> activityRefs =
from acfRef in parent
.Descendants(ns1 + "Folders")
.Descendants(ns1 + "History")
.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,
};

Pete
 
Top