2015-06-02 134 views
1

我正在處理LINQ to XML查詢,如果選擇分支(父節點),則只有特定於該分支的子節點應該突出顯示。 我正在開發一個ASP.NET工具,因爲我需要讀取一個XML文件,它首先根據用戶選擇讀取父節點,它將讀取子節點,現在問題是如果我選擇父節點,它是閱讀完所有的子節點的所有父節點,所以我需要一個查詢中,它應該在選擇分支Linq to 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; 
} 
+0

信息,這就像如果我選擇LionDrop分支,里程碑字段應該只突出WIP2& WIP3,而不是其他里程碑 – tilakraj

+0

該XML無效 - 它缺少一個[根元素](http://en.wikipedia.org/wiki/Root_element)。 – dbc

回答

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

它適用於我,我使用了上述解決方案並根據我的環境進行了修改......非常感謝dbc – tilakraj

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

     } 
    } 
} 
​