2016-08-13 16 views
-3

下面的代碼應該從控制檯的XML文件並顯示結果爲:我需要序列化的XML文件在C#

class Program 
{ 
    static void Main(string[] args) 
    { 
     Program introToVCS = new Program(); 
     System.Xml.Serialization.XmlSerializer reader = new 
      System.Xml.Serialization.XmlSerializer(introToVCS.GetType()); 

     // Read the XML file. 
     System.IO.StreamReader file = 
      new System.IO.StreamReader("Customer.xml"); 

     // Deserialize the content of the file into a Program object. 
     introToVCS = (Program)reader.Deserialize(file); 
     Console.WriteLine(introToVCS); 
     Console.ReadLine();          
    } 
} 
+1

你需要解釋什麼是錯的。 –

+0

請問您可以發佈customer.xml文件嗎? – 2016-08-13 09:44:55

+0

包含文件的路徑名。 – jdweng

回答

0

這應該工作。使用您文件名包括路徑

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Text; 
 
using System.Xml; 
 
using System.Xml.Linq; 
 

 
namespace ConsoleApplication7 
 
{ 
 
    class Program 
 
    { 
 
     const string FILENAME = @"c:\temp\test.xml"; 
 
     static void Main(string[] args) 
 
     { 
 
      XDocument doc = XDocument.Load(FILENAME); 
 
      Console.WriteLine(doc.ToString()); 
 
     } 
 
    } 
 
    
 
}