LINQ to XML:如何从所有元素获取所有属性的集合

Num*_*er8 3 linq-to-xml

我有一些像这样的 XML:

<Section xmlns=\"http:schemas.microsoft.com/winfx/2006/xaml/presentation\"> <Paragraph FontSize=\"12\" FontFamily=\"Arial\" Foreground=\"#FF000000\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\"> <Run FontWeight=\"Normal\" Text=\"space \" /> </Paragraph> </Section>
Run Code Online (Sandbox Code Playgroud)

有没有办法获取所有 FontWeight 属性、所有 FontSize 属性等的集合,而不管父元素如何?

Mar*_*nen 7

假设您有,您可以使用例如获取具有某个名称的XDocument doc = XDocument.Load("file.xml")所有属性或具有特定名称的所有属性。doc.Descendants().Attributes()doc.Descendants().Attributes("FontSize")