Re: LINQ XML Determine Parent Node

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • modify xml file php script
    ... how to create script that modify xml file ... the new parent element should be parent B with its child respectively. ...
    (comp.lang.php)
  • Re: Is my Apache server running as the root user or not?
    ... parent runs as root so that it can attach to port 80. ... port 80, Apache will hand it off to a child process running as www. ... > one of the forks are runned as root and listening on port 80 as well as the other forks ...
    (FreeBSD-Security)
  • Re: Is my Apache server running as the root user or not?
    ... parent runs as root so that it can attach to port 80. ... port 80, Apache will hand it off to a child process running as www. ... > one of the forks are runned as root and listening on port 80 as well as the other forks ...
    (freebsd-questions)
  • Re: multi-level hierarchical structure
    ... "Tom Ellison" wrote: ... You will probably want a query that gives these and Ranks them. ... no parent. ... It needs a root. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Forest to Child -- Permissions
    ... things -- the latter being merely an additional name tag in the parent ... zone servers (if not you need to fix this by removing the true ... "subdomain" and actually delegating or using another method.) ... has conditional forwarding, no recursion, back to the root ...
    (microsoft.public.windows.server.dns)