2017-04-12 34 views
1

請再次需要您的幫助。我轉換.csv文件到一個的XDocument以下方法:在將.csv文件轉換爲XDocument時錯誤的編碼

public static XDocument ConvertCsvToXml(string sourcePath) 
    { 
     string[] lines; 

     try 
     { 
      lines = File.ReadAllLines(sourcePath); 
     } 
     catch (Exception e) 
     { 
      System.Diagnostics.Debug.WriteLine(e.Message); 
      return null; 
     } 

     var headers = lines[0].Split(';').Select(x => x.Trim('\"')).ToArray(); 

     var xml = new XElement("FullInvoice", lines.Where((line, index) => index > 0) 
      .Select(line => new XElement("Line", line.Split(';') 
      .Select((column, index) => new XElement(headers[index], column))))); 

     return new XDocument(xml); 
    } 

這很適合我,但我有一些麻煩與編碼,因爲一些字符(例如「A」,「O」,' ü','ß')以及它們的高層變體在XDocument中是不正確的。

您能不能請教我,我必須在這裏設置編碼以便將這些字符正確地寫入我的XDocument中?

先謝謝你,並從奧地利最好的問候!

回答

0

這個改變對我來說很重要。

lines = File.ReadAllLines(sourcePath, Encoding.Default); 

如果有人面臨同樣的煩惱!