如何通过 linq 访问 XML 节点上的属性?

发布于 2024-12-05 22:24:48 字数 791 浏览 1 评论 0原文

我通读了这个邮政。

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-12-12 22:24:48

Export 元素没有 properties 元素,而这正是您的 GetAttribute 方法尝试查找的内容。

我的猜测是您实际上想要:

var element = xml.Element("Export"); // Just get the first element
var version = (string) element.Attribute("version");

我不清楚为什么您在这里使用查询表达式和委托 - 这只是比您需要的更复杂的事情。但是属性(XName) 可能是您所缺少的......

The Export element doesn't have a properties element, which is what your GetAttribute method is trying to find.

My guess is you actually want:

var element = xml.Element("Export"); // Just get the first element
var version = (string) element.Attribute("version");

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文