2014-05-23 66 views
0

什麼是將以下xml文件讀取到DataTable的有效方法。我無法通過 獲取重複的項目數據。將XML讀取到DataTable

<Employees> 

<Employee> 
    <EmployeeID>25</EmployeeID> 
    <Project> 
     <ProjectId>1001</ProjectId> 
     <STARTDT>2008-08-18</STARTDT> 
    </Project> 
</Employee> 

<Employee> 
    <EmployeeID>26</EmployeeID> 
    <Project> 
     <ProjectId>1002</ProjectId> 
     <STARTDT>2009-11-09</STARTDT> 
     <ENDDT>2013-11-04</ENDDT> 
    </Project> 
    <Project> 
     <ProjectId>1003</ProjectId> 
     <STARTDT>2013-11-05</STARTDT> 
    </Project> 
</Employee> 

</Employees> 

預期的結果

-------------------------------------------------------------------- 
EmployeeId ProjectId  StartDt   EndDt 
------------------------------------------------------------------- 
25    1001  2008-08-18 
26    1002  2009-11-09 
26    1003  2013-11-05 
+0

您可以添加DataSet和Datatable定義的外觀嗎? – rene

+1

您是否將XML加載到「DataTable」中以查找重複項?如果是這樣,你也可以通過'XDocument'使用'LinqToXml'找到它們。 – dash

回答

2

讀入一個DataSet:這裏

String xml = "<Employees><Employee><EmployeeID>1328911000001108</EmployeeID><Project><ProjectId>5000456001090</ProjectId><STARTDT>2008-08-18</STARTDT></Project></Employee><Employee><EmployeeID>1330611000001103</EmployeeID><Project><ProjectId>5017007023913</ProjectId><STARTDT>2009-11-09</STARTDT><ENDDT>2013-11-04</ENDDT></Project><Project><ProjectId>5017007019213</ProjectId><STARTDT>2013-11-05</STARTDT></Project></Employee></Employees>"; 
DataSet ds = new DataSet(); 
ds.ReadXml(new StringReader(xml), XmlReadMode.InferSchema); 

主要是XmlReadMode.InferSchema將產生DataSet中的兩個表,由EmployeeID鏈接。