2013-06-28 59 views
0

,我有以下XML數據:的LINQ to XML查詢到任何結果

<?xml version="1.0" encoding="utf-8" ?> 
    <root> 
    <object name="Object01" table="Table01" pk="Table01_Id"> 
     <table name="Table02" pk="Table02_Id" fk="Table01_Id" include="false"> 
     <table name="Table03" pk="Table03_Id" fk="Table02_Id" include="true"> 
      <table name="Table04" pk="Table04_Id" fk="Table03_Id" include="true"> 
      </table> 
     </table> 
     </table> 
     <table name="Table05" pk="Table05_Id" fk="Table01_Id" include="false"> 
     <table name="Table06" pk="Table06_Id" fk="Table05_Id" include="true" /> 
     </table> 
    </object> 
    <object name="Object02" table="Table07" pk="Table07_Id"> 
     <table name="Table08" pk="Table08_Id" fk="Table07_Id" include="false"> 
     <table name="Table04" pk="Table04_Id" fk="Table08_Id" include="true" /> 
     </table>  
    </object> 
    </root> 

當我運行在LinqPad如下:

var root = XElement.Load("D:\\Foxpert\\FlagChanges\\FlagChanges\\Modified.xml"); 
    var table = root.Descendants("table").Where(e => e.Attribute("name").Value == "Table04") 
    .Select(e => new XElement(e.Parent)).First(); 
table.Dump(); 
    var obj = root.Descendants("object").Where(o => o.Attribute("name").Value == "Object01") 
    .Select(o => new XElement(o)).First(); 
    obj.Dump(); 
    foreach (XElement t in obj.Descendants("table")){ 
    t.Attribute("name").Dump(); 
    t.Parent.Attribute("name").Dump(); 
} 
    table.Attribute("name").Dump(); 
    var parent = obj.Descendants("table") 
    .Where(e => e.Attribute("name")==table.Attribute("name")) 
    .Select(e => new XElement(e.Parent)); 
    parent.Dump(); 

父結果爲空。

有人可以嘗試在LinqPad的代碼,並告訴我爲什麼我的父母是空的?

回答

1
var parent = obj.Descendants("table") 
.Where(e => e.Attribute("name").Value==table.Attribute("name").Value) 
.Select(e => new XElement(e.Parent)); 

e.Attribute("name")table.Attribute("name")不是 「平等」 - 他們只是有相同的值。