views:

28

answers:

1

I have a xml in the below format and need to get the id and name of all the SubEntity when I pass the ParentEntityId. This needs to be done in silverlight.

<Treeview>
    <ParentEntity ParentEntityId="1" ParentEntityName="P1">
        <Facility FacilityId="F1" FacilityName="F1">
            <Category CategoryId="C1" CategoryName="C1"/>             
            <Category CategoryId="C2" CategoryName="C2" >
                <Activity ActivityId="A1" ActivityName="A1" /> 
            </Category>
        </Facility>
        <Facility FacilityId="F2" FacilityName="F2">
            <Category CategoryId="C1" CategoryName="C1">
                <Activity ActivityId="A2" ActivityName="A2" /> 
                <Activity ActivityId="A3" ActivityName="A3" />   
            </Category>
        </Facility>
        <SubEntity SubEntityId="S1" SubEntityName="S1">
            <SubEntity SubEntityId="S2" SubEntityName="S2"/> 
        </SubEntity>
        <SubEntity SubEntityId="S3" SubEntityName="S3">
            <SubEntity SubEntityId="S4" SubEntityName="S4">
                <Facility FacilityId="F3" FacilityName="F3">
                    <Category CategoryId="C1" CategoryName="C1 >
                        <Activity ActivityId="A1" ActivityName="A1" />   
                    </Category>
                </Facility>
            </SubEntity>
            <SubEntity SubEntityId="S5" SubEntityName="S5">
                <Facility FacilityId="F5" FacilityName="F5">
                    <Category CategoryId="C1" CategoryName="C1" />
                    <Category CategoryId="C2" CategoryName="C2"/>                
                </Facility>
            </SubEntity>
            <SubEntity SubEntityId="S6" SubEntityName="S6" />
        </SubEntity>
    </ParentEntity>
    <ParentEntity ParentEntityId="2" ParentEntityName="P2">
        <Facility FacilityId="F1" FacilityName="F1">
            <Category CategoryId="C1" CategoryName="C1"/>                   
        </Facility>
        <SubEntity SubEntityId="S7" SubEntityName="S7" />
    </ParentEntity>
</Treeview>

Assuming I pass my parentEntityId = 1, I need the output S1,S2,S3,S4,S5,S6.

Thanks in advance, Sunitha

A: 

Using LINQ to XML

  foreach(XElement subEntity in doc.Root.Elements("ParentEntity")
       .First(elem => elem.Attribute("ParentEntityID").Value == "1")
       .Descendents("SubEntity") )
  {
     // Do stuff with each SubEntity e.g.:-
     string name = subEntity.Attribute("SubEntityName");
  }

An alternative if you are using Silverlight 4 would be to use XPath instead. You would need to add the System.Xml.XPath.dll to your references. In that case the above loop becomes:-

  foreach(XElement subEntity in doc.Root
      .XPathSelectElements("ParentEntity[ParentEntityId='1']//SubEntity" )
  {
     // Do stuff with each SubEntity e.g.:-
     string name = subEntity.Attribute("SubEntityName");
  }

BTW, You don't in your real XML prefix all your attributes names with the name of the element they placed in do you? E.g., Why "SubEntityName" and not simply "Name"?

AnthonyWJones
Thanks a lot Anthony. This works for me!! And my xml doesnt have an attribuute "SubEntityName". Its just "Name" . :)
Sunitha