LINQ to XML:如何从所有元素中获取所有属性的集合 [英] LINQ to XML: how to get collection of all attributes from all elements
本文介绍了LINQ to XML:如何从所有元素中获取所有属性的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些这样的XML:
I have some XML like this:
<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>
是否有一种方法可以获取所有FontWeight属性,所有FontSize属性等的集合,而与父元素无关?
Is there a way to get a collection of all the FontWeight attributes, all the FontSize attributes, etc., regardless of the parent element?
推荐答案
假设您拥有XDocument doc = XDocument.Load("file.xml")
,则可以使用doc.Descendants().Attributes()
来获取所有属性,或者使用例如来获取特定名称的所有属性. doc.Descendants().Attributes("FontSize")
.
Assuming you have XDocument doc = XDocument.Load("file.xml")
you can get all attributes with doc.Descendants().Attributes()
or all attributes of a certain name with e.g. doc.Descendants().Attributes("FontSize")
.
这篇关于LINQ to XML:如何从所有元素中获取所有属性的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文