C # xml operations: a class for processing XML files (1)

Source: Internet
Author: User
C # xml operations (7)
Chapter 4: common XML processing methods (1)
Since we can use dataset to operate XML files, it is really convenient. He has the ability to use an XML file as a table. Why not?
Therefore, we can operate XML in the same way as a C # class to complete database-like operations:Using system; <br/> using system. text; <br/> using system. io; <br/> using system. XML; <br/> using system. data; </P> <p> namespace xmlbook. com. SEM. tools <br/> {<br/> /// <summary> <br/> // copyright: copyright by sem it department <br/> // version: 0.0.1 <br/> // file: xmlbook. com. SEM. tools. XMLDatabase. CS <br/> // objective: to provide some methods for processing XML as a database <br/> // Author: ouyang yuntian @ <br/> // Email: outrace@soueast-motor.com <br/> // modify: <br />/// </Summary> <br/> public class XMLDatabase <br/> {<br/> # region private member </P> <p> private string strdatafile = NULL; <br/> /// <summary> <br/> // dataset <br/> /// </Summary> <br/> private dataset myds = NULL; <br/> // <summary> <br/> // character filter array, for example, "id = '1' and username = 'track'" <br/> // /</Summary> <br/> private string strfilter = NULL; <br/> /// <summary> <br/> // sort the fields such as "id DESC, username" <br/> /// </Summary> <Br/> private string strsort = NULL; <br/> /// <summary> <br/> // set of field names in the dataset <br/> /// </Summary> <br/> private string [] strfields = NULL; <br/> /// <summary> <br/> // data array in the dataset <br/> /// </Summary> <br/> private string [] strdata = NULL; <br/> /// <summary> <br/> // full path of the template file <br/> /// </Summary> <br/> private string strtemplatefile = NULL; <br/> # endregion </P> <p> # Region Public attributes <br/> /// <summary> <br/> // Template File Path <br/> /// </Summary> <br/> Public String strtemplatefile <br/>{< br/> set {This. strtemplatefile = value ;}< br/> get {return this. strtemplatefile ;} <br/>}< br/> /// <summary> <br/> // data file path <br/> /// </Summary> <br/> public String strdatafile <br/>{< br/> set {This. strdatafile = value ;}< br/> get {return this. strdatafile ;} <br/>}< br/> /// <summary> <br/> // character filtering array <br/> /// </Summary> <br/> public String St Rfilter <br/>{< br/> set {This. strfilter = value ;} <br/>}< br/> /// <summary> <br/> // sort fields <br/> /// </Summary> <br/> public String strsort <br/>{< br/> set {This. strsort = value ;} <br/>}< br/> /// <summary> <br/> // field name in the dataset <br/> /// </Summary> <br/> Public String [] strfields <br/>{< br/> set {This. strfields = value ;} <br/>}< br/> /// <summary> <br/> // data array in the dataset <br/> /// </Summary> <br /> Public String [] Strdata <br/> {<br/> set {This. strdata = value ;}< br/>}< br/> // <summary> <br/> // data set, it can be cached for calling <br/> /// </Summary> <br/> Public dataset myds <br/>{< br/> set {This. myds = value ;}< br/> get {return this. myds ;}< br/>}< br/> # endregion </P> <p> Public XMLDatabase () <br/> {<br/> // todo: provides some methods to process XML as a database <br/> // <br/>}</P> <p> // <summary> <br/> /// get the content of the XML file and enter dataset <br/> /// </Summary> <br/> privat E void open () <br/>{< br/> try <br/>{< br/> This. myds = new dataset (); <br/> filestream fin; <br/> fin = new filestream (this. strdatafile, filemode. open, fileaccess. read, fileshare. readwrite); <br/> This. myds. readxml (FIN); <br/> fin. close (); <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "reading XML data"; <br/> log. strfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/>}</P> <p> // <summary> <br/> // write the operation result to XML. <br/>/ // </Summary> <br/> private void save () <br/>{< br/> try <br/>{< br/> This. myds. writexml (this. strdatafile, xmlwritemode. writeschema); <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "Saving XML data"; <br/> log. s Trfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/>}</P> <p> // <summary> <br/> // obtain a specific data view <br/>/ // generally, when binding data, we can easily generate a bound view <br/> /// </Summary> <br/> /// <returns> data view </returns> <br/> Public dataview selectview () <br/>{< br/> If (this. myds = NULL) This. open (); </P> <p> dataview mydv = new dataview (this. myds. tables [0]); </P> <p> If (strfilte R! = NULL) mydv. rowfilter = This. strfilter; </P> <p> mydv. sort = This. strsort; </P> <p> return mydv; <br/>}</P> <p> // <summary> <br/> // obtain a specific row <br/> // use the row because sometimes, we only need one or more rows of records <br/> /// for example, when we determine the login, we only need the row of an ID, then match the password item <br/> /// </Summary> <br/> /// <returns> all rows of Data </returns> <br/> Public datarow [] selectrows () <br/>{< br/> If (this. myds = NULL) This. open (); <br/> datarow [] myrows = myds. tables [0]. select (Th Is. strfilter); <br/> return myrows; <br/>}</P> <p> /// <summary> <br/> // insert a data entry into the XML file <br/> /// </ summary> <br/> // <returns> whether the operation is successful </returns> <br/> Public bool insert () <br/>{< br/> If (this. myds = NULL) This. open (); </P> <p> try <br/> {<br/> datarow newrow = myds. tables [0]. newrow (); </P> <p> for (INT I = 0; I <this. strfields. length; I ++) <br/>{< br/> newrow [this. strfields [I] = This. strdata [I]; <br/>}</P> <p> Myds. tables [0]. rows. add (newrow); <br/> This. save (); <br/> return true; <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "Writing XML data"; <br/> log. strfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/> return false; <br/>}</P> <p> // <summary> <br/> // update data. In this case, make sure that strfields and strdata The dimensions of the two arrays are the same <br/> /// </Summary> <br/> /// <returns> whether the update is successful </returns> <br/> Public bool update () <br/>{< br/> If (this. myds = NULL) This. open (); </P> <p> try <br/> {<br/> datarow [] editrow = myds. tables [0]. select (this. strfilter); </P> <p> for (Int J = 0; j <editrow. length; j ++) <br/>{< br/> for (INT I = 0; I <this. strfields. length; I ++) <br/>{< br/> editrow [J] [This. strfields [I] = This. strdata [I]; <br/>}< Br/> This. save (); <br/> return true; <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "Update XML data"; <br/> log. strfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/> return false; <br/>}</P> <p> // <summary> <br/> // delete data <br/> // </Summary> <br/> /// <returns> whether the deletion is successful </returns> <br/> Public bool Delete () <br/>{< br/> If (this. myds = NULL) This. open (); </P> <p> try <br/> {<br/> datarow [] editrow = myds. tables [0]. select (this. strfilter); <br/> for (INT I = 0; I <editrow. length; I ++) <br/>{< br/> editrow [I]. delete (); <br/>}< br/> This. save (); <br/> return true; <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "deleting XML data"; <br/> log. Strfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/> return false; <br/>}</P> <p> // <summary> <br/> // based on a template, create an XML file (the premise is that there must be a template file and the target file path is determined) <br/> // </Summary> <br/> // <returns> whether the write is successful </returns> <br/> Public bool create () <br/>{< br/> try <br/> {<br/> xmldocument Doc = new xmldocument (); <br/> xmltextreader reader = new xmltextreader (thi S. strtemplatefile); <br/> Doc. load (Reader); <br/> xmlelement member; <br/> xmlnode root = Doc. documentelement; </P> <p> for (INT I = 0; I <this. strfields. length; I ++) <br/>{< br/> member = Doc. createelement (strfields [I]. tostring (); <br/> member. innertext = This. strdata [I]. tostring (); <br/> root. appendchild (member); <br/>}</P> <p> xmltextwriter xmlwriter = new xmltextwriter (this. strdatafile, null); <br/> xmlw Riter. formatting = formatting. indented; <br/> Doc. save (xmlwriter); </P> <p> xmlwriter. close (); <br/> reader. close (); <br/> return true; <br/>}< br/> catch (exception ee) <br/>{< br/> log = new log (); <br/> log. struser = "system"; <br/> log. strdepartment = "New XML data"; <br/> log. strfilename = "com. SEM. tools. XMLDatabase "; <br/> log. strdescription = ee. message; <br/> log. writelog (); <br/> return false; <br/>}< /P> <p> /// <summary> <br/> // release resources <br/> /// </Summary> <br/> Public void clear () <br/>{< br/> If (this. myds! = NULL) <br/>{< br/> This. myds. dispose (); <br/>}< br/>
Class introduces another log processing class, which will be sent to you here
Using system; <br/> using system. XML; <br/> using system. web; </P> <p> namespace xmlbook. com. SEM. tools <br/> {<br/> /// <summary> <br/> // copyright: copyright by sem it department <br/> // version: 0.0.1 <br/> // file: xmlbook. com. SEM. tools. log. CS <br/> // objective: to provide a log Writing Method (that is, writing errors to an XML record) <br/> // Author: ouyuanning @ <br/> // Email: outrace@soueast-motor.com <br/> // modify: <br/> // </Summary> <br/> public class log <br/> {<br/> # region private member <br/> private httpcontext objcontext = httpcontext. current; <br/> /// <summary> <br/> // log file path <br/> /// </Summary> <br/> private string logfile = NULL; <br/> /// <summary> <br/> // operator <br/> /// </Summary> <br/> private string struser = NULL; <br/> /// <summary> <br/> // Forum <br/> /// </Summary> <br/> private string strdepartment = NULL; <br/> /// <summary> <br/> // name of the file being operated <br/> /// </Summary> <br/> private string strfilename = NULL; <br/> /// <summary> <br/> // operation time <br/> /// </Summary> <br/> private string strtime = NULL; <br/> /// <summary> <br/> // error description <br/> /// </Summary> <br/> private string strdescription = NULL; <br/> # endregion </P> <p> # Region Public attributes <br/> // <summary> <br/> // operator <br/> /// </Summary> <br/> Public String struser <br/> {<br/> get {return this. struser ;}< br/> set {This. struser = value ;} <br/>}< br/> /// <summary> <br/> // name of the file being operated <br/> /// </Summary> <br/> Public String strfilename <br/>{< br/> get {return this. strfilename ;}< br/> set {This. strfilename = value ;} <br/>}< br/> /// <summary> <br/> // Forum <br/> /// </Summary> <br/> Public string strdepartment <br/>{< br/> get {return this. strdepartment ;}< br/> set {This. strdepartment = value ;} <br/>}< br/> /// <summary> <br/> // operation time <br/> /// </Summary> <br/> Public string strtime <br/>{< br/> get {return this. strtime ;}< br/> set {This. strtime = value ;} <br/>}< br/> /// <summary> <br/> // error description <br/> /// </Summary> <br/> Public string strdescription <br/>{< br/> get {return this. strdescription ;}< br/> set {This. strdescription = value ;}< br/>}< br/> # endregion </P> <p> Public log () <br/> {<br/> // todo: provides a log Writing Method (that is, writing errors to XML records) <br/> // <br/>}</P> <p> // <summary> <br/> // write the content to the log file. <br/> /// </Summary> <br/> Public void writelog () <br/>{< br/> This. logfile = This. objcontext. server. mappath (". /log. config "); <br/> try <br/> {<br/> xmldocument Doc = new xmldocument (); <br/> Doc. load (this. logfile); <br/> xmlelement newlog = Doc. createelement ("log"); </P> <p> xmlelement newuser = Doc. createelement ("user"); <br/> newuser. innertext = This. struser; <br/> newlog. appendchild (newuser); </P> <p> xmlelement newdepartment = Doc. createelement ("Department"); <br/> newdepartment. innertext = This. strdepartment; <br/> newlog. appendchild (newdepartment); </P> <p> xmlelement newfilename = Doc. createelement ("FILENAME"); <br/> newfilename. innertext = This. strfilename; <br/> newlog. appendchild (newfilename); </P> <p> xmlelement newtime = Doc. createelement ("time"); <br/> newtime. innertext = datetime. now. tostring (); <br/> newlog. appendchild (newtime); </P> <p> xmlelement newdescription = Doc. createelement ("Description"); <br/> newdescription. innertext = This. strdescription; <br/> newlog. appendchild (newdescription); </P> <p> Doc. documentelement. appendchild (newlog); <br/> Doc. save (this. logfile); <br/>}< br/> catch (exception ex) <br/>{< br/> httpcontext objcontext = httpcontext. current; <br/> objcontext. response. write (ex. message ); <br/>}< br/> finally <br/>{< br/>}< br/>
In this way, we can easily treat an XML file as a table for operation. I will give it in the next article.

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.