2013-07-23 54 views
0

我有一個小問題,我不知道如何導入我的程序讀取的xml文檔,然後驗證字符串(true/false),如果它是真的保存它們。該程序由程序類,paymentImporter類和接口類組成。我知道我只需要更改paymentImporter類以使其工作。代碼介紹如下:導入XML並驗證文件

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

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx – Dan

回答

0

[Serializable()]和相應的XML型裝飾裝飾你PersonPayment類。然後將您的xml文檔反序列化到這些類中並捕獲異常。 How to Deserialize XML document

我看到的唯一問題是您的Payment類中沒有Person字段。您尚未提供您正在反序列化的xml,但如果您將person元素作爲付款子項,則具有相同的類結構將使您更容易。你可以調用Deserialize()並獲得完整的數據結構。