Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML中的乘法元素_C#_Xml - Fatal编程技术网

C# XML中的乘法元素

C# XML中的乘法元素,c#,xml,C#,Xml,首先使用XDocument读取xml文件。然后,通过使用linq,您可以迭代数据集节点的每个子节点,并在linq的select子句中计算总计=数量x价格如下 public void readxml() { XmlDocument doc = new XmlDocument(); string path = @"D:\Project_VS13\FORM\WindowsFormsBTL_1\DATA_BTL.xml"; doc.Load(pat

首先使用
XDocument
读取xml文件。然后,通过使用linq,您可以迭代
数据集
节点的每个子节点,并在linq的
select
子句中计算
总计=数量x价格
如下

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;}
输出:

您更新的XML看起来像

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");
    }
}

AB
17000
10/12/2018
2.
34000使用xml linq:

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

XPath可用于此目的。检查下面的代码。但这相当简单

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(); 
        }
    }
}

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

欢迎使用堆栈溢出。假设您知道如何执行乘法-那么这真的是从元素中提取值,然后在元素中设置值吗?如果是这样的话,我建议阅读LINQtoXML教程,例如。你会从中学到比别人给你一个非常具体的答案要多得多的东西。阅读该教程后,如果您尝试了,但仍然存在问题,请发布您尝试过的内容,我们可以帮助您解决问题。到目前为止,您尝试了哪些内容,您需要什么输出?谢谢您的回复,很抱歉我的回复太晚。我现在正在工作,所以我什么都不能试。我一到家就试试。我必须通过SQL server连接LinQ dtb吗?如果是这样,我就不能使用它,因为我只能使用XML,而不能使用XMLSQL@SamsonWu,如果答案对您有帮助,则在答案左侧打勾,使其变为绿色:)我尝试在Winforms中测试您的解决方案,但在CS0115 CS0103 CS0117等方面失败。接下来,我尝试使用doc.Save(path);将“total的值”覆盖到xml文件,但不起作用。还有什么我可以把它存回去的吗?我现在不在办公室。请复制所有错误。我会尽快解决的。另外,我将尝试为您提供将更新的数据写入xml的代码:)一个建议是,您首先在console应用程序中尝试我的代码,并确保引用消除编译时CS错误所需的所有命名空间,然后通知我。因此,请在windows窗体应用程序中尝试相同的代码,并将所有代码放在一个按钮上单击并通知我。XmlDocument doc=新的XmlDocument();单据加载(路径);我可以用这些行替换Xdocument吗?XmlDocument是较旧的网络库。XDocument是一个使用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(); 
        }
    }
}
XPathDocument document = new XPathDocument("yourxml.xml");
XPathNavigator navigator = document.CreateNavigator();

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