tilakraj
tilakraj

Reputation: 21

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

I'm working on a LINQ to XML query where if I select the branch(parent node), only child nodes which is specific to that branch should highlight. I am developing an ASP.NET tool, in that I need to read an XML file, which reads parent node first, based on user selection, it will read child nodes, now the problem is if I select the parent node, it is reading all the child nodes from all parent node, so I need a query in which it should read respective child node upon selecting the 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 like 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;
}

Upvotes: 1

Views: 227

Answers (2)

dbc
dbc

Reputation: 117263

You can do it with the 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 with the 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();
    }

Upvotes: 0

jdweng
jdweng

Reputation: 34431

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
               "<Root>" +
                "<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>" +
                "</Root>";

            XDocument elements = XDocument.Parse(input);
            string parent = "TigerDrop";
            List<XElement> result = elements.Descendants("branch").Where(item => item.Attribute("name").Value == parent).Descendants("milestone").ToList();

        }
    }
}
​

Upvotes: 0

Related Questions