I have a big problem.I created application in Visual studio on WPF, which can open and read XML files and show data in datagrid. But i cannot find out how to do mathematical operations with XML data.
My CODE
(XAML)
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XMLReader" Height="370" Width="531">
Name="button1" Click="button1_Click" HorizontalAlignment="Right" Width="88" Height="32" VerticalAlignment="Top"/>
(C#)
namespace XMLReader
{
using System.Xml.Serialization;
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void button1_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".xml";
dlg.Filter = "Extended Mark Language (.xml)|*.xml";
Nullable
if (result == true)
{
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
var carlist = XElement.Load(filename);
this.dataGrid1.DataContext = carlist;
}
}
}
}
Example of my XML
.
.
.
So my question is, how to multiply "WithoutDPH " column with "DPH" and show results in datagrid.
(DPH = VAT)
Martin MizerákPosted Oct 30, 2014, 10:21 AM
VulpesPosted Oct 29, 2014, 12:14 PM
if (result == true)
{
string filename = dlg.FileName;
FileNameTextBox.Text = filename;
var carlist = XElement.Load(filename);
foreach(XElement car in carlist.Elements("Car"))
{
double withoutDph;
double.TryParse(car.Attribute("WithountDPH").Value, out withoutDph);
double dph;
double.TryParse(car.Attribute("DPH").Value, out dph);
double withDph = Math.Round(withoutDph * (100.0 + dph) / 100.0, 2);
car.Add(new XAttribute("WithDph", withDph));
}
this.dataGrid1.DataContext = carlist;
}
The in-memory copy of the XML file (though not the file itself) should now include an extra attribute "WithDPH" to which you can bind to show the figure including VAT.