Basically, I'm removing the duplicated entries and summing their values to have only one of each entry.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
const string FILE = "ccc.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILE);
List <XElement> originalInvoices = doc.Descendants("Invoice").ToList();
var groups = originalInvoices.GroupBy(x => (string)x.Element("Hash")).ToList();
var finalInvoices = groups.Select(x => new {
unit = x.Descendants("UnitPrice").Sum(z => (decimal)z),
credit = x.Descendants("CreditAmount").Sum(z => (decimal)z),
tax = x.Descendants("TaxPayable").Sum(z => (decimal)z),
net = x.Descendants("NetTotal").Sum(z => (decimal)z),
gross = x.Descendants("GrossTotal").Sum(z => (decimal)z),
first = x.First()
}).ToList();
foreach (var finalInvoice in finalInvoices)
{
finalInvoice.first.Element("Line").SetElementValue("UnitPrice", finalInvoice.unit);
finalInvoice.first.Element("Line").SetElementValue("CreditAmount", finalInvoice.credit);
finalInvoice.first.Element("DocumentTotals").SetElementValue("TaxPayable", finalInvoice.tax);
finalInvoice.first.Element("DocumentTotals").SetElementValue("NetTotal", finalInvoice.net);
finalInvoice.first.Element("DocumentTotals").SetElementValue("GrossTotal", finalInvoice.gross);
}
doc.Element("SalesInvoices").ReplaceWith(new XElement("SalesInvoices", finalInvoices.Select(x => x.first)));
Console.WriteLine(doc);
Console.ReadKey();
}
}
}
The problem is that my code is exploding when it reaches the following line at the end:
`doc.Element("SalesInvoices").ReplaceWith(new XElement("SalesInvoices", finalInvoices.Select(x => x.first)));`
With the following error:
`An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication2.exe`
I can't fix it my file is this one:
[XML] <?xml version="1.0" encoding="UTF-8"?> <AuditFile xmlns="urn:OECD:StandardAudit - Pastebin.com[
^]
Basically, the code should delete the
Invoices
repeated
Hash
and sum the values of
UnitPrice
,
CreditAmount
,
TaxPayable
,
NetTotal
, and
GrossTotal
, where
UnitPrice
and
Credit
must be the same as
NetTotal
.
So instead of having many
Invoices
with the same
Hash
, I will have only one Invoice with that hash that has the sum of the values above.
The code already removes the duplicated ones and sums the values but somehow I don't know how to fix the problem.
It would be great if someone helps me with the solution for this problem, and I also need to save the "edited" file, which is not happening but my main problem is getting rid of that error.
What I have tried:
const string FILE = "ccc.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILE);
List <XElement> originalInvoices = doc.Descendants("Invoice").ToList();
var groups = originalInvoices.GroupBy(x => (string)x.Element("Hash")).ToList();
var finalInvoices = groups.Select(x => new {
unit = x.Descendants("UnitPrice").Sum(z => (decimal)z),
credit = x.Descendants("CreditAmount").Sum(z => (decimal)z),
tax = x.Descendants("TaxPayable").Sum(z => (decimal)z),
net = x.Descendants("NetTotal").Sum(z => (decimal)z),
gross = x.Descendants("GrossTotal").Sum(z => (decimal)z),
first = x.First()
}).ToList();
foreach (var finalInvoice in finalInvoices)
{
finalInvoice.first.Element("Line").SetElementValue("UnitPrice", finalInvoice.unit);
finalInvoice.first.Element("Line").SetElementValue("CreditAmount", finalInvoice.credit);
finalInvoice.first.Element("DocumentTotals").SetElementValue("TaxPayable", finalInvoice.tax);
finalInvoice.first.Element("DocumentTotals").SetElementValue("NetTotal", finalInvoice.net);
finalInvoice.first.Element("DocumentTotals").SetElementValue("GrossTotal", finalInvoice.gross);
}
doc.Element("SalesInvoices").ReplaceWith(new XElement("SalesInvoices", finalInvoices.Select(x => x.first)));
Console.WriteLine(doc);
Console.ReadKey();
}
}
}