导入XML并验证文件

本文关键字:文件 验证 XML 导入 | 更新日期: 2023-09-27 18:24:34

我有一个小问题,我不知道如何导入我的程序要读取的xml文档,然后验证字符串(true/false),如果是true,请保存它们。该程序由程序类、paymentImporter类和接口类组成。我知道我只需要更改paymentImporter类就可以了。代码如下所示:

付款进口商类别:

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);

}

}

程序类别:

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);
        }

导入XML并验证文件

使用[Serializable()]和相应的xml类型装饰器装饰PersonPayment类。然后只需将xml文档反序列化到这些类中并捕获异常。如何反序列化XML文档

我看到的唯一问题是在Payment类中没有Person字段。您还没有提供要反序列化的xml,但如果您将person元素作为支付的子元素,那么具有相同的类结构将使您更容易。您只需调用Deserialize()就可以获得完整的数据结构。