[英]How to get Single XElement object using Linq to Xml?
I would like to use Linq to Xml to get a single XElement from a .xml file by attribute name, similar to how you retrieve single objects in Linq to Sql by Id below: 我想使用Linq to Xml通过属性名从.xml文件中获取单个XElement,类似于在下面通过Id检索Linq to Sql中的单个对象:
var singleDog = context.Dogs.Single(p => p.Id == int.Parse(Id));
Is this possible? 这可能吗?
Absolutely. 绝对。 Just use something like: 只需使用以下内容:
xdoc.Descendants()
.Where(x => x.HasAttribute("id") && x.Attribute("id")==id)
.Single();
There may be a more efficient way of doing it, admittedly... 无可否认,可能有一种更有效的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.