2017-08-08 67 views
1

我使用下面的代碼通過標記名獲取內容控件。C#+通過標記名稱獲取所有/多個控件控件+ open xml

// Gets content control of the sub table by tag. 
var sdtSubTable = 
    mainPart.Document.Descendants<SdtElement>() 
    .Where(s => s.SdtProperties.GetFirstChild<Tag().Val.Value.Contains("tagname")); 

但是,這只是一個返回控制,但是我有多個同名內容控件,我需要檢索所有使用LINQ。

LINQ將節省時間,因此使用它,否則將循環所有的內容控制,然後找到匹配的。

對此有何幫助?

+2

LINQ還必須遍歷所有內容。希望你的意思是它會保持_readability_。除此之外,你必須分享你的XML看起來像,因爲現在看起來它會返回多個。 – krillgar

回答

0

找到解決方案。

List<SdtBlock> sdtSubTable = mainPart.Document.Body.Descendants<SdtBlock>().Where 
    (r => r.SdtProperties.GetFirstChild<Tag>().Val.Value.Contains("tagname")).ToList();