2012-03-23 63 views
0

我MEAM例如,我可以得到如何让所有节点及其属性的XML使用LINQ

var collection = from c in el.Descendants("current_conditions") 
       select c; 

唯一节点 - current_conditions其属性。

但如何获得其他节点(forecast_conditions)没有新的查询再次(是否有可能在一个查询?)? XML文档看起来像这样

<current_conditions> 
<condition data="Clear"/> 
<temp_f data="36"/> 
<temp_c data="2"/> 
<humidity data="Humidity: 70%"/> 
<icon data="/ig/images/weather/sunny.gif"/> 
<wind_condition data="Wind: N at 9 mph"/> 
</current_conditions> 
<forecast_conditions> 
    <day_of_week data="Fri"/> 
    <low data="28"/> 
    <high data="54"/> 
    <icon data="/ig/images/weather/mostly_sunny.gif"/> 
    <condition data="Mostly Sunny"/> 
</forecast_conditions> 
+3

el.Elements(); ? – Default 2012-03-23 22:03:39

回答

1

如果为根元素你得到的根目录下的所有元素节点做doc.Element。

var xElement = doc.Element(rootElementName); 

要获得任何元素下的所有元素节点,你可以使用元素()

XElement elem = xElement.Element(anyElementName); 
    IEnumerable<XElement> nodeCollection = from allNodes in elem.Elements() 
                  select allNodes; 
+1

为什么不只是'nodeCollection = elem.Elements();'? – Default 2012-03-23 22:25:15

相关问题