XML中的乘法元素

时间:2018-12-08 08:58:43

标签: c# xml

<DATASET>
<DS>
    <Name>AB</Name>
    <Price>17000</Price>
    <Dates>10/12/2018</Dates>
    <Quantity>2</Quantity>
    <Total>0</Total>
    <Notes></Notes>
</DS>
<DS>    
    <Name>CD</Name>
    <Price>20000</Price>
    <Dates>10/12/2018</Dates>
    <Quantity>3</Quantity>
    <Total>0</Total>
    <Notes></Notes>
</DS>
</DATASET>

我要计算总计=数量*价格。如何在XML和C#中实现

当前,我已将xml文件列出到datagridview。那么如何计算总数,然后将其写回到xml文件?我尝试了doc.save(path),但没有用。

public void readxml()
    {
        XmlDocument doc = new XmlDocument();
        string path = @"D:\Project_VS13\FORM\WindowsFormsBTL_1\DATA_BTL.xml";
        doc.Load(path);
        DataSet tab = new DataSet();
        tab.ReadXml(path);
        DataTable dtb = tab.Tables["DS"];  
        DataTable dt_view = new DataTable();
        //dt_view = dtb.Clone();
        XDocument doc1 = XDocument.Load(path);
        //new column
        dt_view.Columns.Add("Item Name");
        dt_view.Columns.Add("Price (USD)");
        dt_view.Columns.Add("Order Date");
        dt_view.Columns.Add("Ordered Quantity");
        dt_view.Columns.Add("Totals");
        dt_view.Columns.Add("Notes");
        foreach (DataRow dr in dtb.Rows)
        {
                dt_view.Rows.Add(dr.ItemArray);
        }
        dataGridView1.DataSource = dt_view;}

3 个答案:

答案 0 :(得分:0)

首先使用XDocument阅读您的xml文件。然后,通过使用linq,您可以遍历DATASET节点的每个子节点,并在linq的select子句中可以像计算Total = Quantity x Price

class program
{
    public static void Main()
    {
        XDocument doc = XDocument.Load(@"Path to your xml file");

        var result = from d in doc.Descendants("DATASET").Elements("DS")
                     select new
                     {
                         Name = d.Element("Name")?.Value,
                         Price = d.Element("Price")?.Value,
                         Dates = d.Element("Dates")?.Value,
                         Quantity = d.Element("Quantity").Value,
                         Total = d.Element("Quantity") != null && d.Element("Price") != null ? Convert.ToInt32(d.Element("Quantity").Value) * Convert.ToInt32(d.Element("Price").Value) : 0,
                         Notes = d.Element("Notes")?.Value,
                     };

        XDocument newDoc =
            new XDocument(
                new XElement("DATASET",
                result.Select(x => new XElement("DS",
                    new XElement("Name", x.Name),
                    new XElement("Price", x.Price),
                    new XElement("Dates", x.Dates),
                    new XElement("Quantity", x.Quantity),
                    new XElement("Total", x.Total),
                    new XElement("Notes", x.Notes)))));

        newDoc.Save(@"updated.xml");
    }
}

输出:

您更新的XML外观如下。

<?xml version="1.0" encoding="utf-8"?>
<DATASET>
  <DS>
    <Name>AB</Name>
    <Price>17000</Price>
    <Dates>10/12/2018</Dates>
    <Quantity>2</Quantity>
    <Total>34000</Total>      <= (34000 = 2 * 17000)
    <Notes></Notes>
  </DS>
  <DS>
    <Name>CD</Name>
    <Price>20000</Price>
    <Dates>10/12/2018</Dates>
    <Quantity>3</Quantity>
    <Total>60000</Total>      <= (60000 = 3 * 20000)
    <Notes></Notes>
  </DS>
</DATASET>

答案 1 :(得分:0)

使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            int price = doc.Descendants("DS").Select(x => (int)x.Element("Price") * (int)x.Element("Quantity")).Sum(); 
        }
    }
}

答案 2 :(得分:0)

XPath可以用于此目的。检查下面的代码。不过,这很简单。

XPathDocument document = new XPathDocument("yourxml.xml");
XPathNavigator navigator = document.CreateNavigator();

Double total = (double)navigator.Evaluate("sum(//DATASET/DS/(Price*Quantity))");

PS:此操作需要XPath 2.0或更高版本。而且它可以在任何平台上运行