2013-07-23 2 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 문서를 이러한 클래스로 deserialize하고 예외를 catch합니다. How to Deserialize XML document

유일한 문제는 Payment 클래스에 Person 필드가 없다는 것입니다. 역 직렬화 할 xml을 제공하지 않았지만 동일한 요소를 사용하여 지불 요소로 person 요소를 사용하면 쉽게 사용할 수 있습니다. Deserialize()를 호출하고 완전한 데이터 구조를 얻을 수 있습니다.

관련 문제