2012-10-16 71 views
0

我有以下xml.What我需要做的是,我想在每個表中group行使用他們的locid使用linq。使用Linq分組Xml元素

<taxdata schema="3" nextrowid="812" timesaved="0" calcmode="1" calcneeded="0"> 
    <table name="aatbl_entity" id="1" ver="102" ddtime="11-10-2012 17:57:08" locs="" /> 
    <table name="aatbl_entityactivity" id="2" ver="102" ddtime="11-10-2012 17:57:08" locs="" key0="2"> 
     <row name="" locid="100" rows="71" rowid="71" Ks0="1"> </row> 
     <row name="" locid="48" rows="52" rowid="52" Ks0="8"> </row> 
     <row name="" locid="6148" rows="216" rowid="216" Ks0="16"> </row> 
     <row name="" locid="6148" rows="217" rowid="217" Ks0="17"> </row> 
     <row name="" locid="6148" rows="218" rowid="218" Ks0="18"> </row> 
     <row name="" locid="6154" rows="219" rowid="219" Ks0="19"> </row> 
     <row name="" locid="6154" rows="220" rowid="220" Ks0="20"> </row> 
     <row name="" locid="6154" rows="221" rowid="221" Ks0="21"> </row> 
     <row name="" locid="6154" rows="222" rowid="222" Ks0="22"> </row> 
     <row name="" locid="6154" rows="223" rowid="223" Ks0="23"> </row> 
     <row name="" locid="6209" rows="224" rowid="224" Ks0="24"> </row> 
     <row name="" locid="6127" rows="233" rowid="233" Ks0="32"> </row> 
     <row name="" locid="6127" rows="234" rowid="234" Ks0="33"> </row> 
     <row name="" locid="6127" rows="235" rowid="235" Ks0="34"> </row> 
     <row name="" locid="36" rows="236" rowid="236" Ks0="35"> </row> 
     <row name="" locid="36" rows="244" rowid="244" Ks0="43"> </row> 
     <row name="" locid="111" rows="266" rowid="266" Ks0="51"> </row> 
     <row name="" locid="111" rows="752" rowid="752" Ks0="59"> </row> 
     <row name="" locid="6192" rows="361" rowid="361" Ks0="55"> </row> 
     <row name="" locid="10" rows="810" rowid="810" Ks0="60"> </row> 
     <row name="" locid="10" rows="811" rowid="811" Ks0="61"> </row> 
     <row name="" locid="10" rows="812" rowid="812" Ks0="62"> </row> 
    </table> 
<table name="aatbl_entityactivitydetail" id="13" ver="102" ddtime="11-10-2012 17:57:09" locs="" key0="2" key1="5"> 
     <row name="" locid="6327" rows="90" rowid="90" Ks0="1" Ks1="1"> </row> 
     <row name="" locid="6327" rows="104" rowid="104" Ks0="1" Ks1="2"> </row> 
     <row name="" locid="6327" rows="118" rowid="118" Ks0="1" Ks1="3"> </row> 
     <row name="" locid="6327" rows="133" rowid="133" Ks0="1" Ks1="4"> </row> 
     <row name="" locid="6327" rows="522" rowid="522" Ks0="3" Ks1="1"> </row> 
     <row name="" locid="6327" rows="529" rowid="529" Ks0="3" Ks1="2"> </row> 
     <row name="" locid="6327" rows="536" rowid="536" Ks0="3" Ks1="3"> </row> 
     <row name="" locid="6327" rows="543" rowid="543" Ks0="3" Ks1="4"> </row> 
     <row name="" locid="101" rows="177" rowid="177" Ks0="1" Ks1="5"> </row> 
     <row name="" locid="101" rows="563" rowid="563" Ks0="3" Ks1="5"> </row> 
     <row name="" locid="105" rows="185" rowid="185" Ks0="1" Ks1="6"> </row> 
     <row name="" locid="105" rows="564" rowid="564" Ks0="3" Ks1="6"> </row> 
     <row name="" locid="106" rows="186" rowid="186" Ks0="1" Ks1="7"> </row> 
     <row name="" locid="106" rows="565" rowid="565" Ks0="3" Ks1="7"> </row> 
     <row name="" locid="106" rows="2" rowid="2" Ks0="3" Ks1="7"> </row> 
    </table> 
    <table name="aatbl_entityactivityasset" id="14" ver="102" ddtime="11-10-2012 17:57:09" locs="" key0="2" key1="9" 
</taxdata> 

我嘗試下面鑽沒有工作,

const string filePath = @"D:\XJH\11-10-2012_17_57_08.xml"; 

XDocument doc = XDocument.Load(filePath); 

var tableIds = 
    doc 
     .Descendants("table") 
     .Select(table => new { id = table.Attributes("id").ToString() }); 

foreach (var id in tableIds) 
{ 
    var rowgroups =doc.Descendants("table") 
        .Elements() 
        .Where(row => row.Ancestors() 
          .Attributes("id").ToString() == id.ToString()) 
        .GroupBy(row => new { ids = row.Attributes("locid") }) 
        .Select(row => row); 
} 

我不知道這種方法進行分組是否正確與否,請幫助....

+0

請勿混用'foreach'和LINQ。你能讓我們知道輸出應該是什麼樣子嗎? – Enigmativity

+1

格式化代碼時,請注意橫向滾動有點痛苦 - 巨大的單行很煩人,因此我重新格式化了代碼。 –

+0

請注意,你已經在循環中聲明瞭一個變量,但沒有使用它 - 你怎麼知道它「沒有工作」? –

回答

1

這是你想要的東西:

var query = 
    from xe in doc.Element("taxdata").Elements("table") 
    from gr in xe.Elements("row").GroupBy(_ => _.Attribute("locid").Value) 
    select new 
    { 
     id = xe.Attribute("id").Value, 
     locid = gr.Key, 
     rows = gr.Count(), 
    }; 

我得到以下結果:

Results

+0

謝謝...那就是我真正想要的.. :) –

1

嘗試更換你的代碼是這樣的:

string[] tableIds = doc.Descendants("table").Select(t => t.Attribute("id").Value).ToArray<string>(); 

foreach (var id in tableIds) 
{ 
    var rowgroups = doc.Descendants("table").Elements() 
     .Where(row => row.Parent.Attribute("id").Value.Equals(id)) 
     .GroupBy(row => row.Attribute("locid").Value) 
     .Select(row => row); 
    // ... do something ... 
    // Here you will have all the Grouped XElements in rowgroups 
} 
+0

感謝回覆..我會檢查這一點... –

0

你可以試試這個,我覺得它的工作原理:

var xe = doc.Descendants("table") 
      .Elements("row") 
      .GroupBy(a => a.Attribute("locid").Value) 
      .Select(a => new { Value = a.Key, Count = a.Count() });