0

I have a slight problem, i dont know how to import the xml document i have for my program to read, and then to validate the string (true/false) and if it is true save them. The program consists of program class, paymentImporter class and a interfaces class. I know i just need to change the paymentImporter class to get this working. the code is presented below:

paymentImporter class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WsPreInterviewTest
{

    public class XmlPaymentImporter : IPaymentImporter
    {
        public XmlPaymentImporter(IDatabaseProvider service_provider)
        {
            //import file

            throw new NotImplementedException();
        }

        public bool ReadFile(string file_path, List<string> errs)
        {

            //read file for validation

            throw new NotImplementedException();
        }

        public bool SaveAll()
        {
            //save if validation ok

            throw new NotImplementedException();
        }
    }
}

Interface Class:




public class Person
{
    public int Recno { get; set; }
    public string Name {get;set;}        
}

public class Payment
{
    public int PersonRecno { get; set; }
    public string Currency { get; set; }
    public decimal Amount { get; set; }
    public string Narrative { get; set; }

}

interface IPaymentImporter
{
    bool ReadFile(string file_path, List<string> errs);
    bool SaveAll();
}

public interface IDatabaseProvider
{
    DatabaseRepository GetDataRepository();
}

public abstract class DatabaseRepository
{
    public DatabaseRepository()
    {

    }


    /// <summary>
    /// returns null if person is not known
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public abstract Person FindPerson(string name);

    public abstract bool SavePayment(Payment payment);


}

}

program class:

class Program
{
    static void Main(string[] args)
    {
        IDatabaseProvider _service_provider = null;
        string example_file = "ExampleFile.xml";
        XmlPaymentImporter importer = new XmlPaymentImporter(_service_provider);
        List<string> errors = new List<string>();
        if (importer.ReadFile(example_file, errors))
        {
            if (!importer.SaveAll())
                Console.WriteLine("Problem saving records");
        }
        else
        {
            Console.WriteLine("Problem With File");
            foreach (string s in errors)
                Console.WriteLine(s);
        }
bandaa
  • 159
  • 3
  • 5
  • 11

1 Answers1

0

Decorate your Person and Payment classes with [Serializable()] and corresponding xml type decorators. Then just deserialize your xml document into these classes and catch exceptions. How to Deserialize XML document

The only problem I see is that there's no Person field in your Payment class. You have not provided the xml you are deserializing, but if you have the person element as a child of payment, having the same class structure will make it easier for you. You can just call Deserialize() and get the complete data structure.

Community
  • 1
  • 1
Taras Dzyoba
  • 121
  • 3