Asp. NET read-write XML file

Source: Internet
Author: User
Tags object serialization

the approach in this article is primarily to implement a read-write XML file using serialization and deserialization, noting that the Web. config file, which is not a specified one, refers to a custom config file. The following is an XML read-write operation class, containing read methods used in the deserialization mode, as well as writing methods, using the serialization method.
Using system;using system.io;using system.web;using system.xml.serialization;namespace WX_Tools{//<summary> XML file read/write class///</summary> public class Xmlreadwrite {//<summary>///based on relative path and full file name Read XML file, need to pass in entity object type deserialization method//</summary>//<param name= "FilePath" > Relative path (/aa/aa/) </param&gt        ; <param name= "obj" > Entity object Type </param>//<param name= "filename" > file name (a.txt) </param>/            <returns> return Object </returns> public Object Read (string FilePath, Object obj, string fileName) {            Get physical path FilePath = HttpContext.Current.Server.MapPath (FilePath); Determine if the path exists, does not exist create path if (directory.exists (FilePath)) {FilePath = Path.Combine (filepat                h, FileName);                According to the path, put the file into the file stream FileStream filestream=null; try {fileStream = newFileStream (FilePath, FileMode.Open, FileAccess.Read, fileshare.readwrite); XmlSerializer XmlSerializer = new XmlSerializer (obj.                    GetType ());                Return Xmlserializer.deserialize (FileStream); } catch (Exception ex) {new Debuglog (). Bugwritetxt (ex.                ToString ());                        } finally {if (FileStream! = null) {                    Filestream.close ();        }}} return null; }///<summary>///To generate an XML file based on a relative path that requires an incoming entity object serialization method///</summary>//<param NA Me= "FilePath" > Relative path (/aa/aaa/) </param>//<param name= "obj" > Entity object </param>///<param N        Ame= "FileName" > Full name of File (a.config) </param> public void Write (string FilePath, Object obj, String fileName)          {//Get physical path   FilePath = HttpContext.Current.Server.MapPath (FilePath);            Determine if the path exists, there is no creation path directoryisexists (FilePath);            FilePath = Path.Combine (FilePath, fileName);            FileStream FileStream = null; try {fileStream = new FileStream (FilePath, FileMode.Create, FileAccess.Write, Fileshare.readwri                TE); XmlSerializer XmlSerializer = new XmlSerializer (obj.                GetType ());            Xmlserializer.serialize (FileStream, obj); } catch (Exception ex) {new Debuglog (). Bugwritetxt (ex.            ToString ()); } finally {if (FileStream! = null) {filestream.cl                OSE (); }}}///<summary>///To determine if the XML folder exists, if it does not exist, create this path, note that the path passed is a physical path///</sum mary>//<param name= "FilePath" > Physical path </param> private void directoryisexists(String FilePath) {if (!            Directory.Exists (FilePath)) {directory.createdirectory (FilePath); }        }    }}

In matching these methods, you also need to create a corresponding entity model, which is used when invoking the method. For example, to invoke a read method, you need to pass in a relative path (/aa/aa/), an Entity object type, and a full file name (a.config), which returns an object that you can convert to the object type you want.
If you want to invoke the Write method, note that this method can be created automatically without creating a directory or creating a file. The same only needs to pass in the relative directory you need (/AA/AAA), and the entity object with the data, and the name of the file you want to generate (A.config), and then execute this method, create the directory and the file, the config file will be the field of the entity object as a node, help you generate XML data formats.



Welcome to the. NET Technology Exchange Group

Asp. NET read-write XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.