Linq to Xml:在选择父节点时突出显示相应的子节点

时间:2015-06-02 19:20:45

标签: c# xml linq-to-xml

我正在进行LINQ to XML查询,如果我选择分支(父节点),则只应突出显示特定于该分支的子节点。 我正在开发一个ASP.NET工具,我需要读取一个XML文件,它首先读取父节点,根据用户选择,它将读取子节点,现在问题是如果我选择父节点,它正在读取来自所有父节点的所有子节点,所以我需要一个查询,在选择分支时它应该读取相应的子节点

<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>

我试过这样,

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

2 个答案:

答案 0 :(得分:0)

试试这个

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();

        }
    }
}
​

答案 1 :(得分:0)

您可以使用以下LINQ查询执行此操作:

    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();
    }

或使用以下XPath查询:

    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();
    }