2014-05-15 3 views
0

클래스 개체 청크를 파일로 읽어 들이고 같은 내용을 편집 할 수있는 XML Parser를 작성하려고합니다. 펄스는 단지 클래스 ID 및 이름과 유사한 클래스xml에 개체를 읽거나 쓰는 방법은 무엇입니까?

public class Category 
{ 
    //Unique identifier for a category 
    string _Id; 
    public string Id 
    { 
     get { return _Id; } 
     set { _Id = value; } 
    } 

    //Name of the category, for reference of the client 
    string _name; 
    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 

    //URI where the category is hosted 
    string _uri; 
    public string Uri 
    { 
     get { return _uri; } 
     set { _uri = value; } 
    } 

    //Collection of pulses in this category 

    public ICollection<Pulse> Pulses; 

    public Category() 
    { 
     _Id = null; 
     _name = null; 
     _uri = null; 
     Pulses = new List<Pulse>(); 
    } 
} 

다음 카테고리 클래스이다

[Serializable] 
public class Service 
{ 
    public enum ServiceStatus { ACTIVE, INACTIVE, SUSPENDED }; 

    //Unique identifier for a service 
    string _Id; 
    public string Id 
    { get{ return _Id;} 
     set{ _Id = value;} 
    } 

    //Name of the service, for reference of the client 
    string _name; 
    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 

    //URI where the service is hosted 
    string _uri; 
    public string Uri 
    { 
     get { return _uri; } 
     set { _uri = value; } 
    } 

    //The state of the service 
    ServiceStatus _status; 
    public ServiceStatus Status 
    { 
     get { return _status;} 
     set { _status = value; } 
    } 

    //the categories contained in the service instance 

    public ICollection<Category> Categories; 

    //collection of users/clients who can access this service 
    public ICollection<Client> Clients; 


    public Service() 
    { 
     _Id = null; 
     _name = null; 
     _uri = null; 
     _status = ServiceStatus.ACTIVE; 
     Categories = new List<Category>(); 
     Clients = new List<Client>(); 
    } 
} 

다음

클래스 구조이다.

xml 파일에서 이러한 개체를 읽고 쓰는 가장 좋은 방법은 무엇입니까? 작업이 무거워 읽었습니다 가능한 배열을 인덱싱 된 또는 사전으로 가능한 액세스 할 수있는 값을 읽으려면 원하는 있지만 모든 양식이 좋습니다.

친절하게 가장 간단한 방법을 제안합니다. 저는 C#에서 XML 클래스의 수에 압도당했습니다. #

+5

'XmlSerializer' 클래스와 결합 된 클래스 및 속성 데코레이터를 사용하십시오. 시작하려면이 기사와 같은 기사를 읽는 것이 좋습니다. http://msdn.microsoft.com/en-us/library/vstudio/2baksw0z(v=vs.100).aspx –

답변

0

DataSet으로 변환하고 작업을 수행하는 것은 어떻습니까? XmlTextReader.ReadXml (reader);

+0

몇 가지 시도 ... 하지만 엉망이 됐어. 당신이 자세히 설명 할 수 있다면 아주 좋을거야. –

0

DataSet ds = new DataSet(); ds.ReadXml (fileNamePath);

이렇게하면 모든 XML이 DataSet으로 변환됩니다. XML을 DataSet으로 변환 한 후에는 원하는 모든 작업을 수행 할 수 있습니다.

관련 문제