如何编辑这些代码以便编写invoiceDataOut.xml?

时间:2017-06-20 05:14:36

标签: c# xml visual-studio

“invoiceUnit.Items.Add(itemOne);”部分我收到一个错误。它说:  1. System.NullReferenceException:'对象引用未设置为     一个对象的实例。'  2. PROGRAM_B2.Program.Order.Items.get返回null。

当我注释掉那行代码时,我设法让它运行但invoiceDataOut.xml中的数据什么都没有。序列化器似乎没有工作。

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

    }

}

请让我知道如何让它变得更好。

0 个答案:

没有答案