【问题标题】:I need to serialize an xml file in c#我需要在c#中序列化一个xml文件
【发布时间】:2016-12-20 06:07:38
【问题描述】:

以下代码应该从 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();                                    
    }
}

【问题讨论】:

  • 你需要解释什么是错的。
  • 能否请您发布 customer.xml 文件。
  • 包含文件的路径名。

标签: c# xml serialization


【解决方案1】:

这应该可行。使用你的文件名,包括路径

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

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多