2012-03-20 19 views
4

次のコードを記述して、指定されたxmlファイルを読み込み、その内容をデータテーブルに書き込みます。これはレガシーアプリケーションであるため、LinqToXmlを使用するように提案しないでください。XMLファイルをDataTableに読み込むコード

  // create the DataTable that will hold the data 
      DataTable table = new DataTable("ListOfPersonsWithInfo"); 


      // open the file using a Stream 
      using (Stream stream = new FileStream(fileNameWithAbsolutePath, FileMode.Open, FileAccess.Read)) 
      { 
       // create the table with the appropriate column names 
       table.Columns.Add("Name", typeof(String)); 
       table.Columns.Add("ImagePath", typeof(String)); 
       table.Columns.Add("Address", typeof(String)); 

       // use ReadXml to read the XML stream 
       table.ReadXml(stream); 

       // tried with this overload-option as well but didnt help 
       //table.ReadXml(fileNameWithAbsolutePath); 

       // return the results 
       return table; 
      } 

ただし、返されるテーブルにはゼロ行が含まれています... !!!実際のxmlファイルは「3行」を持っているし、次のように構成されているとおりである(すべてのアイデアは間違ってここで何が起こっている?):

<?xml version="1.0" encoding="utf-8"?> 
<Details> 
    <EachPerson> 
     <Name>Jack</Name> 
     <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName> 
     <Address>NewYork</Address> 
    </EachPerson> 
    <EachPerson> 
     <Name>Tom</Name> 
     <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName> 
     <Address>London</Address> 
    </EachPerson> 
    <EachPerson> 
     <Name>Jill</Name> 
     <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName> 
     <Address>Tokyo</Address> 
    </EachPerson> 
</Details> 

答えて

11

あなたがReadXmlの説明

 DataSet ds = new DataSet(); 
     ds.ReadXml(fileNameWithAbsolutePath); 
     return ds.Tables[0]; 
+0

ザッツそれPraVnを使用することができます。..しかし、なぜdataTABLE.ReadXml()が失敗しても、dataSET.ReadXml()はなぜ機能するのかについてはまだ分かりません。このReadXmlの内部実装に固有のものがあります。 – MukeshAnAlsoRan

+1

はい。

は、データテーブルにロードできない3つのレベルを持つルートです – PraveenVenu

+0

Thanks PraVn ... ADO.NETの内部動作を非常に明確に把握しているようです。それがあなたに合っている場合には、私の他の質問を見ることができますか?を参照してください。この質問は誰も答えようとしていないか、あまりにもあいまいであるかもしれませんし、誰かがダイアグラムで答えるのに時間がかかりすぎるかもしれません。 – MukeshAnAlsoRan

関連する問題