通过属性vlaue从xml获取数据 [英] Getting data from xml by attribute vlaue

查看:74
本文介绍了通过属性vlaue从xml获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的文件(myfile.xml).我必须将所有内容(包括产品节点)归入具有id=1的产品.

I have a file with the following content (myfile.xml). I have to get all content coming under (including product node) a product with id=1.

<products>  
  <product id="1">
       <category>q</category>  
  </product>    
  <product id="2">      
       <category>w</category>    
  </product>   
  <product id="3">       
  <category>e</category>   
 </product>
</products>`

即结果应该是:

 <product id="1"> 
      <category>q</category>
  </product> 

我该怎么做?

推荐答案

var root = XElement.Load("path to the file");
var node = root.Descendants("product").FirstOrDefault(e=>e.Attribute("id").Value == "1");

这篇关于通过属性vlaue从xml获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆