为了账号安全,请及时绑定邮箱和手机立即绑定

如何在ASP.Net中通过id从xml中读取特定数据

如何在ASP.Net中通过id从xml中读取特定数据

C#
蓝山帝景 2023-12-17 16:57:33
我想根据其 id 选择特定数据,这里是 xml 的示例<?xml version="1.0" encoding="ISO-8859-1"?><productdetails>    <product>        <Id>1</Id>        <product_name>banana</product_name>        <product_price>5.00</product_price>        <product_description>its banana</product_description>        <product_quantity>12</product_quantity>    </product>    <product>        <Id>2</Id>        <product_name>mango</product_name>        <product_price>10.00</product_price>        <product_description>its mango</product_description>        <product_quantity>12</product_quantity>    </product>    <product>        <Id>3</Id>        <product_name>orange</product_name>        <product_price>3.00</product_price>        <product_description>its orange</product_description>        <product_quantity>1</product_quantity>    </product>    <product>        <Id>4</Id>        <product_name>apple</product_name>        <product_price>4.00</product_price>        <product_description>its apple</product_description>        <product_quantity>1</product_quantity>    </product></productdetails>我想做的是将它们存储回另一个不同但临时的 xml 文件中,然后在 GridView 中进行计算。
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

希望我正确理解您的询问。您可以使用 Linq 根据其 Id 搜索特定数据。例如,


var root = XElement.Parse(xmlString);  

var idToSearch = 1;

var result= root.Elements("product")

                .Where(x=>Convert.ToInt32(x.Element("Id").Value)==idToSearch)

                .Select(x=>new 

                           {

                             ProductName=x.Element("product_name").Value,

                             Price=x.Element("product_price").Value

                           });


查看完整回答
反对 回复 2023-12-17
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信