C# Linq到Xml:在选择父节点时高亮显示相应的子节点

C# Linq到Xml:在选择父节点时高亮显示相应的子节点,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在处理一个LINQ到XML的查询,如果我选择分支(父节点),只有特定于该分支的子节点应该高亮显示。 我正在开发一个ASP.NET工具,因为我需要读取一个XML文件,该文件首先读取父节点,根据用户选择,它将读取子节点,现在的问题是,如果我选择父节点,它将读取所有父节点的所有子节点,因此我需要一个查询,在该查询中,它应在选择分支时读取相应的子节点 我试过这样做 public List get里程碑() { 字符串inputFilePath=Server.MapPath(@“~/DropLis

我正在处理一个LINQ到XML的查询,如果我选择分支(父节点),只有特定于该分支的子节点应该高亮显示。 我正在开发一个ASP.NET工具,因为我需要读取一个XML文件,该文件首先读取父节点,根据用户选择,它将读取子节点,现在的问题是,如果我选择父节点,它将读取所有父节点的所有子节点,因此我需要一个查询,在该查询中,它应在选择分支时读取相应的子节点


我试过这样做

public List get里程碑()
{
字符串inputFilePath=Server.MapPath(@“~/DropList.xml”);
var elements=XDocument.Load(inputFilePath);
var result=(来自elements.subjects(“里程碑”).subjects(“里程碑”)。其中(item=>(string)item==“branch”)
.SelectMany(item=>item.Parent.Elements(“里程碑”).Elements(“里程碑”)).ToList();
返回结果;
}
试试这个

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

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument elements=XDocument.Parse(输入);
字符串parent=“TigerDrop”;
列表结果=元素.子体(“分支”).Where(item=>item.Attribute(“name”).Value==parent.subjections(“里程碑”).ToList();
}
}
}
​
试试这个

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

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument elements=XDocument.Parse(输入);
字符串parent=“TigerDrop”;
列表结果=元素.子体(“分支”).Where(item=>item.Attribute(“name”).Value==parent.subjections(“里程碑”).ToList();
}
}
}
​

您可以通过以下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();
    }
公共静态列表GetMilestoneNames(XDocument doc,string branchName)
{
var query=doc.Root.substands(“分支”)
其中(e=>e.Attributes(“name”).Any(a=>a.Value==branchName))
.要素(“里程碑”)
.要素(“里程碑”)
.Attributes(“name”)。选择(a=>a.Value);
返回query.ToList();
}
或使用以下查询:

公共静态列表GetMilestoneNames(XDocument doc,string branchName)
{
var query=(IEnumerable)doc.xpatheevalue(string.Format(“//branch[@name='{0}']]/millements/millements/@name”,branchName));
return query.Cast().Select(a=>a.Value.ToList();
}

您可以通过以下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();
    }
公共静态列表GetMilestoneNames(XDocument doc,string branchName)
{
var query=doc.Root.substands(“分支”)
其中(e=>e.Attributes(“name”).Any(a=>a.Value==branchName))
.要素(“里程碑”)
.要素(“里程碑”)
.Attributes(“name”)。选择(a=>a.Value);
返回query.ToList();
}
或使用以下查询:

公共静态列表GetMilestoneNames(XDocument doc,string branchName)
{
var query=(IEnumerable)doc.xpatheevalue(string.Format(“//branch[@name='{0}']]/millements/millements/@name”,branchName));
return query.Cast().Select(a=>a.Value.ToList();
}

关于信息,就像我选择了LionDrop branch,里程碑字段应该只突出显示WIP2和WIP3,而不是其他MileStores XML无效——它缺少一个。关于信息,就像我选择了LionDrop branch,里程碑字段应该只突出显示WIP2和WIP3,不是其他milestonesThat XML无效--它缺少一个。关于信息,这就像我选择LionDrop branch,里程碑字段应该只突出显示WIP2和WIP3,不是其他milestonesThat XML无效--它缺少一个。它对我有用,我使用了上面的解决方案,并根据我的环境进行了修改。。。非常感谢dbcit为我工作,我使用了上述解决方案并根据我的环境进行了修改。。。非常感谢dbcit为我工作,我使用了上述解决方案并根据我的环境进行了修改。。。非常感谢dbc