0

I need to export data into XML file, using XSD. There are many examples how to do it, but most of them do not show how to popuate the actual data, but to save the object as an XML. The one I could find didn't work for me.

1) I use an xsd file of Agresso http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.html which I have successfully downloaded and generated a class with xsd.exe command.

2) I have added this class to my project. ABWInvoice is the class for the complexType element Invoice. The xml may contain more than one invoice, hence its maxOccurs is set to "unbounded". Each Invoice can have InvoiceNo element and Header complex element.

3) I have started to write the code and first thought I can use a list, as the number of invoices is dynamic. But List<ABWInvoice> list = new ABWInvoice(); didn't work "Cannot implicitly convert type 'abc.Agresso.ABWInvoice' to 'System.Collections.Generic.List'", so I have decided to at least try to have one record and go from there, but oAgresso.Invoice[0].Header fails in runtime with System.NullReferenceException: 'Object reference not set to an instance of an object.'

 private void CreateXMLHeader()
    {
        var oAgresso = new ABWInvoice { };

        oAgresso.Invoice[0] = new ABWInvoiceInvoice
        { InvoiceNo = "1" };
        oAgresso.Invoice[0].Header = new ABWInvoiceInvoiceHeader()
        {
            OrderRef = "5678",
            InvoiceDate = Date.Now
        };




        //var agressoXMLImport = Shared.XMLHelper.ReadXml<ABWInvoice>(@"E:\temp\ABW_Invoice_Test.xml");
        Shared.XMLHelper.SaveXml<ABWInvoice>(oAgresso, @"e:\temp\ABW_Export_Test.xml");

    }

Can you advise on how 1) build a dynamic array (I do not know the amount of invoices, when I start building the XML; 2)What is wrong with my current code?

Much appreciated!

Abdollah
  • 4,579
  • 3
  • 29
  • 49
KDWolf
  • 398
  • 2
  • 14
  • Are you asking [What is a list in C# and how does it work?](https://stackoverflow.com/q/15768685) If so see maybe http://csharp.net-informations.com/collection/list.htm or [How to initialize a C# string list (List) with many string values](https://stackoverflow.com/q/3139118/3744182) or [How can I initialize a C# List in the same line I declare it. (IEnumerable string Collection Example)](https://stackoverflow.com/q/4438169/3744182) Otherwise I think we will need to see your generated c# types (i.e. a [mcve]) to give a concrete answer. – dbc Feb 09 '18 at 19:37
  • Usually you just have to add .ToList() to the linq. Other solution is to change From : List list = new ABWInvoice();To : var list = new ABWInvoice(); – jdweng Feb 09 '18 at 20:07
  • dbc, thank you for the reply - I know what is the list, but I cannot use it in my case: List list = new ABWInvoice(); it says that cannot convert. There is no option to add ToList() either. At least I couldn't find it. The class itself is very big to publish here, but it can be easily downloaded via the link to Agresso above. My second question was my mu current approach throws the exception. Can you advise on this one, please? – KDWolf Feb 09 '18 at 20:21
  • You can only add ToList() to an array object or a List(). You are doing new ABWInvoice which is singular. You should do : List list = new List(); – jdweng Feb 10 '18 at 06:41
  • Thank you, jdweng. It did work, yet I still cannot proceed, as getting same System.NullReferenceException error. What else do I miss, you think? – KDWolf Feb 10 '18 at 11:41

1 Answers1

1

Member arrays need to be initialized with known size, so its easier to make a List of ABWInvoiceInvoice then populate it with your data by using add method and at the end assign whole list to your member array

private void CreateXMLHeader()
    {
        var oAgresso = new ABWInvoice { };
        List<ABWInvoiceInvoice> invlist = new List<ABWInvoiceInvoice>();
        invlist.Add(new ABWInvoiceInvoice { InvoiceNo = "1" ,
        Header= new ABWInvoiceInvoiceHeader()
        {
            OrderRef = "5678",
            InvoiceDate = DateTime.Now
        }
        });
        oAgresso.Invoice = invlist.ToArray();
Mohamad Elnaqeeb
  • 541
  • 4
  • 13
  • Thank you, Mohamad. Much appreciated! I presume you have already understood I am newbie to C#. May I trouble you with the next issue I have here, please? After successfully applying your suggestion I was able to get the list. In addition to Header the invoice also has Details complexType element. – KDWolf Feb 12 '18 at 20:35
  • When I tried same approach, building listDetails for a given invoice and then trying to apply Header = new ABWInvoiceInvoiceHeader() { LineNo = 1, InvoiceDate = Date.Now, DueDate = Date.Now, DueDateSpecified = true, OrderRef = "9876", Currency = "GBP" }, Details = listDetails, I obviously got " cannot convert Details[] into listDetails<>" Can you advise how to usually address this problem, please? – KDWolf Feb 12 '18 at 20:35
  • you need to add ToArray() converter - Details = listDetails.ToArray() – Mohamad Elnaqeeb Feb 12 '18 at 20:55
  • Works like a charm. Thank you again. As I am doing ToArray() here and there - will it significantly affect my performance and is there better, faster way I should consider you think (I appreciate it might require to start all over again though)? – KDWolf Feb 12 '18 at 21:35