如何:从 XML 文件中读取类数据

来源:互联网 发布:网络与新媒体考研吧 编辑:程序博客网 时间:2024/04/29 14:56

此示例使用 XmlSerializer类的 Deserialize 方法读取存储在名为 IntroToVCS.xml 的示例文件中的对象上的数据。

public class Book{    public string title;    static void Main()    {        Book introToVCS  = new Book();        System.Xml.Serialization.XmlSerializer reader = new        System.Xml.Serialization.XmlSerializer(introToVCS.GetType());        // Read the XML file.        System.IO.StreamReader file=             new System.IO.StreamReader("c:\\IntroToVCS.xml");        // Deserialize the content of the file into a Book object.        introToVCS = (Book) reader.Deserialize(file);        System.Windows.Forms.MessageBox.Show(introToVCS.title,            "Book Title");    }}

以下条件可能会导致异常:

  • 路径名可能太长。


原创粉丝点击