如何通过 linq 访问 XML 节点上的属性?
我通读了这个邮政。
我有这个 XML:
<?xml version="1.0" encoding="utf-8" ?>
<Export version="" srcSys="" dstSys="" srcDatabase="" timeStamp="">
</Export>
这是我尝试过的,但没有运气:
var xml = XElement.Parse(BuyingModule.Properties.Resources.Export);
Func<XElement, string, string> GetAttribute = (e, property) => e.Elements("property").Where(p => p.Attribute("name").Value == property).Single().Value;
var query = from record in xml.Elements("Export")
select record;
var prop = GetAttribute(query.FirstOrDefault(), "version");
如何访问“导出”节点的属性?
我需要设置这些属性
I read through this post.
I have this XML:
<?xml version="1.0" encoding="utf-8" ?>
<Export version="" srcSys="" dstSys="" srcDatabase="" timeStamp="">
</Export>
This is what i tried, but with no luck:
var xml = XElement.Parse(BuyingModule.Properties.Resources.Export);
Func<XElement, string, string> GetAttribute = (e, property) => e.Elements("property").Where(p => p.Attribute("name").Value == property).Single().Value;
var query = from record in xml.Elements("Export")
select record;
var prop = GetAttribute(query.FirstOrDefault(), "version");
How do i access to properties
of the "Export" Node?
I need to set those properties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Export
元素没有properties
元素,而这正是您的GetAttribute
方法尝试查找的内容。我的猜测是您实际上想要:
我不清楚为什么您在这里使用查询表达式和委托 - 这只是比您需要的更复杂的事情。但是
属性(XName)
可能是您所缺少的......The
Export
element doesn't have aproperties
element, which is what yourGetAttribute
method is trying to find.My guess is you actually want:
It's not clear to me why you've used a query expression and a delegate here - it's just things more complicated than you need. But
Attribute(XName)
is probably what you were missing...