zoukankan      html  css  js  c++  java
  • How to: Write Object Data to an XML File

    This example writes the object from a class to an XML file using the XmlSerializer class.

    Namespace:   System.Xml.Serialization
    Assembly:  System.Xml (in System.Xml.dll)

    Example

    This code example defines a class named Book, creates an instance of the class, and uses XML serialization to write the instance to an XML file.

    Code similar to this is available as an IntelliSense code snippet. In the Code Snippet Manager, it is located in XML. For more information, see Code Snippets.

    public class XMLWrite
    {
    
       static void Main(string[] args)
        {
            WriteXML();
        }
    
    
        public class Book
        {
            public String title; 
        }
    
    
        public static void WriteXML()
        {
            Book overview = new Book();
            overview.title = "Serialization Overview";
            System.Xml.Serialization.XmlSerializer writer = 
                new System.Xml.Serialization.XmlSerializer(typeof(Book));
    
            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//SerializationOverview.xml";
            System.IO.FileStream file = System.IO.File.Create(path);
    
            writer.Serialize(file, overview);
            file.Close();
        }
    }

    Compiling the Code

    The class must have a public constructor without parameters.

    Robust Programming

    The following conditions may cause an exception:

    .NET Framework Security

    This example creates a new file, if the file does not already exist.

    If an application needs to create a file, that application needs Create access for the folder.

    If the file already exists, the application needs only Write access, a lesser privilege.

    Where possible, it is more secure to create the file during deployment, and only grant Read access to a single file, rather than Create access for a folder.

  • 相关阅读:
    一维数组的相关问题
    逗号表达式
    三目表达式
    前自增和后自增的比较
    关于Spring中的PagedListHolder分页类的分析
    fmt:formatDate标签的输出格式
    用java流方式判断文件类型
    常用文件的文件头(附JAVA测试类)
    jsp页面判断文件上传类型
    spring MVC上传文件演示
  • 原文地址:https://www.cnblogs.com/chucklu/p/5145520.html
Copyright © 2011-2022 走看看