2013-08-20 9 views
0

는 여러 개의 XML 파일에서 데이터를 읽으려고하지만, 다음과 같은 예외가 발생 해요 :ADO.NET - 오류 읽는 여러 XML 파일

"예기치 않은 XML 선언 XML 선언은 첫 번째 노드해야합니다. 문서, 공백 문자가 나타나지 않습니다. 줄 11895, 위치 3 " 이 경우

는, 내가 루프를 읽을려고 3 개 개의 파일이 있습니다. 각 파일을 개별적으로 읽으면 제대로 작동합니다. 파일이 루프에서 연속적으로 읽힐 때만 두 번째 파일을 읽는 동안 예외가 발생합니다. 위의 예외에서 file1은 11895 개의 행을 가지므로 file2를 읽을 때 각 파일에 자체 XML 선언이 있으므로 '11895 행의 예기치 않은 XML 선언'이 throw됩니다.

내 질문은 : 새 데이터 집합 및 MemoryStream을 개체가 각각의 파일을 읽는 데 사용되는 경우에, 왜 2 층과 3 파일을 XML 선언 헤더를 가지고 허용되지 않는 이유는 무엇입니까? 이전의 책과 독 립적으로 읽는 방법은 무엇입니까? 당신의 데이터 집합은 한 번에 하나의 XML 파일을 읽을 수 있습니다처럼

//Open the database connection 
using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ApplicationServices)) 
{ 
    cn.Open(); 
    // Begin a new transaction 
    using (SqlTransaction tr = cn.BeginTransaction()) 
    { 
     try 
     { 
      //Loop through each attachment, convert the attachment xml to DataTable and Load into the Database 
      foreach (Attachment att in message.Attachments) 
      { 
       LogMessage(string.Format("Reading Attachment: {0}", att.Name), 0); 
       // Load the Contents of the attachment into a MemoryStream 
       using (MemoryStream ms = new MemoryStream(att.Content, true)) 
       { 
        ms.Seek(0, System.IO.SeekOrigin.Begin); 
        //Use a dataset to automatically determine the schema from the XML file 
        using (DataSet ds = new DataSet()) 
        { 
         //Load the MemoryStream contents into a DataSet, that automatically determines the schema 
         try 
         { 
          ds.ReadXml(ms); 
          ms.Dispose(); 
         } 
         catch (Exception ex) 
         { 
          LogMessage(string.Format("Error reading xml {0}. {1}{2}", att.Name, ex.Message, ex.StackTrace), 2); 
          throw ex; 
         } 

         LogMessage(string.Format("Found {0} records", ds.Tables[0].Rows.Count), 0); 

         /*Other business logic to process data in the ds.Tables[0] ... */ 
        } 
       } 
      } 

      //Commit transaction if everything worked out fine 
      LogMessage("Card product import complete, committing transaction", 0); 
      tr.Commit(); 
     } 
     catch (Exception ex) 
     { 
      LogMessage("Error Occured during card product import, rolling back transaction", 2); 
      tr.Rollback(); 
      throw ex; 
     } 
    } 
    cn.Close(); 
} 

답변

0

그것은 같습니다

여기 내 코드입니다.

ds.ReadXml(ms); 

XmlReader를 통해 3 개의 xml 파일을 모두 가져올 수 있다면 시도해보십시오. 그것은 당신에게 뭔가를 말해 줄 것입니다.

+0

나는 XmlDocument.Parse (MemoryStream을)를 시도했지만 그 역시 같은 문제가 있습니다! – Nishant