我需要在c#中序列化一个xml文件

时间:2016-08-13 09:31:22

标签: c# xml serialization

以下代码应该从XML文件中读取并在控制台中显示结果:

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 :(得分: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());
        }
     }
 
}