写入文件时,三行被删除,不应删除 [英] When writing file 3 lines are deleted, and should not

查看:59
本文介绍了写入文件时,三行被删除,不应删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在删除一个XML文件中的重复条目,并且代码正在删除它们并将这些值相加,以使每个发票只有一个条目. 但是,当到达末尾时,它将删除以下三行:

I am removing the duplicated entries in one XML file, and the code is removing them and summing the values to have only one entry of each invoice. But when it reaches the end it removes 3 lines the following ones:

      <NumberOfEntries>11972</NumberOfEntries>
      <TotalDebit>0</TotalDebit>
      <TotalCredit>34422.86</TotalCredit>

所以没有像这样的最终文件:

So instead of having the final file like:

...
      <SourceDocuments>
        <SalesInvoices>
          <NumberOfEntries>11972</NumberOfEntries>
          <TotalDebit>0</TotalDebit>
          <TotalCredit>34422.86</TotalCredit>
          <Invoice>
            <InvoiceNo>FS 006120180101/19959</InvoiceNo>
...

它看起来像:

...
      <SourceDocuments>
        <SalesInvoices>
          <Invoice>
            <InvoiceNo>FS 006120180101/19959</InvoiceNo>
...

我的代码如下:

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

namespace ConsoleApplication2
{
    class Program
    {

        //Ficheiro 
        const string FILENAME = "ccc.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XNamespace ns = doc.Root.Name.Namespace;



            List<XElement> originalInvoices = doc.Descendants(ns + "Invoice").ToList();

            var groups = originalInvoices.GroupBy(x => (string)x.Element(ns + "Hash")).ToList();

            var finalInvoices = groups.Select(x => new
            {
                unit = x.Descendants(ns + "UnitPrice").Sum(z => (decimal)z),
                credit = x.Descendants(ns + "CreditAmount").Sum(z => (decimal)z),
                tax = x.Descendants(ns + "TaxPayable").Sum(z => (decimal)z),
                net = x.Descendants(ns + "NetTotal").Sum(z => (decimal)z),
                gross = x.Descendants(ns + "GrossTotal").Sum(z => (decimal)z),
                first = x.First()
            }).ToList();

            foreach (var finalInvoice in finalInvoices)
            {
                finalInvoice.first.Element(ns + "Line").SetElementValue(ns + "UnitPrice", finalInvoice.unit);
                finalInvoice.first.Element(ns + "Line").SetElementValue(ns + "CreditAmount", finalInvoice.credit);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "TaxPayable", finalInvoice.tax);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "NetTotal", finalInvoice.net);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "GrossTotal", finalInvoice.gross);
            }

            doc.Descendants(ns + "SalesInvoices").FirstOrDefault().ReplaceWith(new XElement(ns + "SalesInvoices", finalInvoices.Select(x => x.first)));
            doc.Descendants(ns + "SalesInvoices").
            Console.WriteLine(doc);
            doc.Save("Root.xml");
            Console.ReadKey();

        }
    }
    }

您可以在这里看到我的XML文件的示例: Pastebin链接

And you can see a sample of my XML file here: Pastebin Link

有人可以帮我这个忙,我怎样才能不删除这三行呢? 问题可能出在写文件的最后一行,但是我不确定.

Can someone help me with this, how can I make it to not remove those 3 lines? The problem is probably on the last line where it writes the file, but I'm not sure.

doc.Descendants(ns + "SalesInvoices").FirstOrDefault().ReplaceWith(new XElement(ns + "SalesInvoices", finalInvoices.Select(x => x.first)));

有关此问题的小更新: 好吧,我真的认为问题出在上面的行上,因为如果我将SalesInvoices更改为TotalCredit,这是正在消失的文件的最后一行,则仍然是错误的,而不是:

Small Update on the question: Well I really think that the problem is on the line above because if I change for example SalesInvoices with TotalCredit which is the last line of the ones that are disappearing the file still wrong but instead of:

...
      <SourceDocuments>
        <SalesInvoices>
          <Invoice>
            <InvoiceNo>FS 006120180101/19959</InvoiceNo>
...

我得到:

...
      <SourceDocuments>
        <SalesInvoices>
          <NumberOfEntries>11972</NumberOfEntries>
          <TotalDebit>0</TotalDebit>
          <TotalCredit>
          <Invoice>
            <InvoiceNo>FS 006120180101/19959</InvoiceNo>
...

标签<Invoice>之前缺少34422.86</TotalCredit>

,并在第一个封闭元素</Invoice>之后添加</TotalCredit>,您可以在此处进行测试:链接以测试代码

and it's adding the </TotalCredit> after the first closed element </Invoice> as you can test here: Link to test the code

推荐答案

易于修复.只需添加缺少的3个元素:

Easy to fix. Just add the missing 3 elements :

            foreach (var finalInvoice in finalInvoices)
            {
                finalInvoice.first.Element(ns + "Line").SetElementValue(ns + "UnitPrice", finalInvoice.unit);
                finalInvoice.first.Element(ns + "Line").SetElementValue(ns + "CreditAmount", finalInvoice.credit);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "TaxPayable", finalInvoice.tax);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "NetTotal", finalInvoice.net);
                finalInvoice.first.Element(ns + "DocumentTotals").SetElementValue(ns + "GrossTotal", finalInvoice.gross);
            }

            XElement salesInvoices = doc.Descendants(ns + "SalesInvoices").FirstOrDefault();
            XElement numberOfEntries = salesInvoices.Element(ns + "NumberOfEntries");
            XElement totalDebit = salesInvoices.Element(ns + "TotalDebit");
            XElement totalCredit = salesInvoices.Element(ns + "TotalCredit");

            salesInvoices.ReplaceWith(new XElement(ns + "SalesInvoices", new object[] {
                numberOfEntries,
                totalDebit,
                totalCredit,
                finalInvoices.Select(x => x.first)
            }));

这篇关于写入文件时,三行被删除,不应删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆