c# - Linq to Xml : highlighting respective child nodes upon selecting parent node -


i'm working on linq xml query if select branch(parent node), child nodes specific branch should highlight. developing asp.net tool, in need read xml file, reads parent node first, based on user selection, read child nodes, problem if select parent node, reading child nodes parent node, need query in should read respective child node upon selecting branch

<branch name="tigerdrop">    <milestones>      <milestone name="beta1"></milestone>      <milestone name="beta2"></milestone>    </milestones> </branch> <branch name="eagledrop">    <milestones>      <milestone name="rfld"></milestone>      <milestone name="rfvd"></milestone>    </milestones> </branch> <branch name="liondrop">    <milestones>      <milestone name="wip2"></milestone>      <milestone name="wip3"></milestone>    </milestones> </branch> 

i have tried this,

public list<string> getmilestones() {    string inputfilepath = server.mappath(@"~/droplist.xml");    var elements = xdocument.load(inputfilepath);    var result = (from item in elements.descendants("milestones").descendants("milestone").where(item => (string) item == "branch")        .selectmany(item => item.parent.elements("milestones").elements("milestone"))).tolist();      return result; } 

you can following linq query:

    public static list<string> getmilestonenames(xdocument doc, string branchname)     {         var query = doc.root.descendants("branch")             .where(e => e.attributes("name").any(a => a.value == branchname))             .elements("milestones")             .elements("milestone")             .attributes("name").select(a => a.value);          return query.tolist();     } 

or following xpath query:

    public static list<string> getmilestonenames(xdocument doc, string branchname)     {         var query = (ienumerable)doc.xpathevaluate(string.format("//branch[@name='{0}']/milestones/milestone/@name", branchname));         return query.cast<xattribute>().select(a => a.value).tolist();     } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -