使用Linq从在线XML文件中读取数据(Use Linq to read data from an online XML file)

编程入门 行业动态 更新时间:2024-10-28 12:28:51
使用Linq从在线XML文件中读取数据(Use Linq to read data from an online XML file)

我一直在寻找一种从C#中的XML文件中读取特定数据的方法。

示例XML文件: 此处 。

我正在尝试创建一个方法,给定一个id属性(例如“17392”,参见示例),可以返回该特定类型id的最大购买价格。

我一直在寻找几个小时,说实话,我已经超过了我的头脑。 任何帮助表示赞赏。

I've been looking for a way to read specific data from an XML file in C#.

Example XML file: here.

I'm trying to make a method that, given an id attribute (such as "17392", see example), can return the max buy price for that specific type id.

I've been looking for a couple hours, and to be honest I'm in over my head. Any help is appreciated.

最满意答案

为我工作

XDocument xdoc = XDocument.Load(@"MyFile.xml"); var lv1s = from lv1 in xdoc.Descendants("type") .Where(l => (string) l.Attribute("id") == "17392") .Descendants("buy") select (string)lv1.Element("max"); string Result = ""; foreach (var lv1 in lv1s) { Result = lv1.ToString(); }

解释:它的作用首先加载xml文件,然后根据你的要求循环遍历其后代(“ type ”),其属性(“ id ”)为17392 ,并从中向下一级以选择后代(“ 购买 “)并从购买它选择最大元素。简而言之,它只是遵循你的树的结构

Worked for me

XDocument xdoc = XDocument.Load(@"MyFile.xml"); var lv1s = from lv1 in xdoc.Descendants("type") .Where(l => (string) l.Attribute("id") == "17392") .Descendants("buy") select (string)lv1.Element("max"); string Result = ""; foreach (var lv1 in lv1s) { Result = lv1.ToString(); }

Explaination: What it does it first loads the xml file,then it loops through its descendants("type") where its attribute ("id") is 17392 as per your requirement and from that it goes one level down to select descendants ("buy") and from buy it selects the max element.In short it just follows the structure of your tree

更多推荐

本文发布于:2023-04-29 04:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335289.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在线   文件   数据   XML   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!