The part at the "invoiceUnit.Items.Add(itemOne);" I get an error. It says: 1. System.NullReferenceException: 'Object reference not set to an instance of an object.' 2. PROGRAM_B2.Program.Order.Items.get returned null.
Also when I comment out that line of codes, I managed to get it run but the data in invoiceDataOut.xml is nothing. The serializer doesn't seem to be working.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace PROGRAM_B2
{
// This is the class that we want to serialize:.
[Serializable()]
public class Program
{
// Main class which is directly accessed
public class Order
{
public int InvoiceID { get; set; }
private DateTime _InvoiceDate { get; set; }
public string InvoiceDate {
get { return _InvoiceDate.ToString("d/m/yyyy"); }
set { _InvoiceDate = DateTime.ParseExact(value, "d/m/yyyy", CultureInfo.InvariantCulture); }
}
public int SellerID { get; set; }
public int BuyerID { get; set; }
public int OrderID { get; set; }
public List<Item> Items { get; set; }
public double ShippingCharges { get; set; }
public double InvoiceTotalCost { get; set; }
}
// Indirectly accessed class
public class Item
{
public int ItemID { get; set; }
public string ItemName { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public double NewUnitPrice { get; set; }
}
static void Main(string[] args)
{
Order invoiceUnit = new Order();
// Simple Type - Main Data
invoiceUnit.InvoiceID = 0011995;
invoiceUnit.InvoiceDate = "12/5/2017";
invoiceUnit.SellerID = 0020;
invoiceUnit.BuyerID = 1231;
invoiceUnit.OrderID = 9021;
// An Item -Multiple
Item itemOne = new Item();
itemOne.ItemID = 0001;
itemOne.ItemName = "Apple Macbook Pro";
itemOne.Description = "The best professional laptop for professionals.";
itemOne.Quantity = 5;
itemOne.NewUnitPrice = 4950.50;
// Add Item
invoiceUnit.Items.Add(itemOne);
// An Item -Multiple
Item itemTwo = new Item();
itemTwo.ItemID = 0002;
itemTwo.ItemName = "Microsoft Surface Laptop";
itemTwo.Description = "The most versatile professional laptop for experts.";
itemTwo.Quantity = 10;
itemTwo.NewUnitPrice = 3500.90;
// Add Item
invoiceUnit.Items.Add(itemTwo);
// Simple Type - Footer Data
invoiceUnit.ShippingCharges = 7000.00;
invoiceUnit.InvoiceTotalCost = 19500.10;
// Create a new XmlSerializer instance with the type of the test class
XmlSerializer SerializerObj = new XmlSerializer(typeof(Program));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"../../../invoiceDataOut.xml");
SerializerObj.Serialize(WriteFileStream, invoiceUnit);
// Cleanup
WriteFileStream.Close();
/*
The test.xml file will look like this:
<?xml version="1.0"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeString>foo</SomeString>
<Settings>
<string>A</string>
<string>B</string>
<string>C</string>
</Settings>
</TestClass>
*/
// Test the new loaded object:
/*Console.WriteLine(invoiceUnit.someString);
foreach (string Setting in invoiceUnit.Settings)
{
Console.WriteLine(Setting);
}
Console.ReadLine();
*/
}
}
}
Please let me know how I can make it better.