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

我想根据其 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 中进行计算。


蓝山帝景
浏览 85回答 1
1回答

函数式编程

希望我正确理解您的询问。您可以使用 Linq 根据其 Id 搜索特定数据。例如,var root = XElement.Parse(xmlString);&nbsp;&nbsp;var idToSearch = 1;var result= root.Elements("product")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x=>Convert.ToInt32(x.Element("Id").Value)==idToSearch)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x=>new&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ProductName=x.Element("product_name").Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Price=x.Element("product_price").Value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP