How to write object data to an XML file (C#)
This example writes the object from a class to an XML file using the XmlSerializer class.
Example
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 being serialized must have a public constructor without parameters.
Robust Programming
The following conditions may cause an exception:
The class being serialized does not have a public, parameterless constructor.
The file exists and is read-only (IOException).
The path is too long (PathTooLongException).
The disk is full (IOException).
.NET 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.
See also
Povratne informacije
Pošalјite i prikažite povratne informacije za