C # pairs XML operation: a class that handles XML files (1)

xiaoxiao2021-03-05  19

C # Operation XML Preliminary (7) Chapter 4: General XML Processing Method (1) Since we can use Dataset to operate XML files, it is too convenient, he is fully capable of use an XML file as a table as a table. What is it not? So we can operate XML with such a Class Class to complete similar data-like operations: USING SYSTEM;

Using system.text;

Using system.io;

USING SYSTEM.XML;

Using system.data;

Namespace xmlbook.com.sem.tools

{

///

/// copyright: Copyright by SEM IT Department

/// version: 0.0.1

/// File: xmlbook.com.sem.tools.xmldatabase.cs

/// Objective: Provide some methods that use XML as a database processing

/// Author: BBC Days @ 2005-04-09

/// Mailbox: Outrace@soueast-motor.com

/// Modify:

///

Public Class Xmldatabase

{

#Region private member

Private string strdatafile = NULL;

///

/// data set

///

Private dataset myds = null;

///

/// Character filter array, such as "ID = '1' and username = 'trace'"

///

Private string strfilter = null;

///

/// Sort field such as "ID desc, username"

///

Private string strsort = NULL;

///

/// Data collection in the field name collection

///

PRIVATE STRING [] strfields = NULL;

///

/// data set in data collection

///

PRIVATE STRING [] strdata = null;

///

/// Template file full path

///

Private string strtemplatefile = null;

#ndregion

#Region Public property

///

/// Template file path

///

Public String StrtemplateFile

{

Set {this.strtemplatefile = value;

Get {return this.strtemplatefile;}

}

///

/// data file path

///

Public String StrDatafile

{

Set {this.strdatafile = value;

Get {return this.strdatafile;

}

///

/// Character filter number / //

Public String Strfilter

{

Set {this.strfilter = value;

}

///

/// Sort field

///

Public String Strsort

{

Set {this.strsort = value;

}

///

/// field name in the data collection

///

Public String [] strfields

{

Set {this.strfields = value;

}

///

/// data set in data collection

///

Public string [] strdata

{

Set {this.strdata = value;

}

///

/// Data collection can be placed in a cache for call

///

Public DataSet MyDS

{

Set {this.myds = value;

Get {return this.myds;}

}

#ndregion

Public xmldatabase ()

{

//

// TODO: Provides some methods that use XML as a database processing

//

}

///

// / get the content of the XML file and fill in DataSet

///

Private void open ()

{

Try

{

THIS.MYDS = New Dataset ();

FileStream Fin;

FIN = New filestream (this.StrDataFile, FileMode.open, FileAccess.Read, Fileshare.Readwrite);

this.myds.readxml (FIN);

Fin.close ();

}

Catch (Exception EE)

{

LOG log = new log ();

Log.struser = "system";

Log.StrDepartment = "Read XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

}

}

///

/// Write the results to XML

///

Private void save ()

{

Try

{

this.myds.writexml (this.strdatafile, XMLWRITE.WRITESCHEMA);

}

Catch (Exception EE)

{

LOG log = new log ();

Log.struser = "system";

Log.StrDepartment = "Save XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

}

}

///

/// get a specific data view

/// Generally, we can make it easy to generate a binding view when it is bound to be bound.

///

/// Data View public dataview selectView ()

{

IF (this.myds == null) .open ();

DataView MyDV = New DataView (this.myds.tables [0]);

IF (strfilter! = null) mydv.rowfilter = this.strfilter;

Mydv.sort = this.strsort;

Return MYDV;

}

///

// / get a specific line

/// The way you use the line is because sometimes we only need a row or multiple lines of records.

///, for example, when we judge the landing, you just need the line of a ID, then match its password item.

///

/// Data

Public DataRow [] SELECTROWS ()

{

IF (this.myds == null) .open ();

DataRow [] myrows = myds.tables [0] .select (this.strfilter);

Return myrow;

}

///

/// Insert a data into XML

///

/// Whether the operation is successful

Public bool insert ()

{

IF (this.myds == null) .open ();

Try

{

DataRow newrow = myds.tables [0] .newrow ();

For (int i = 0; i

{

NEWROW [this.strfields [i]] = this.strdata [i];

}

MYDS.TABLES [0]. Rows.Add (newrow);

THIS.SAVE ();

Return True;

}

Catch (Exception EE)

{

LOG log = new log ();

Log.struser = "system";

Log.StrDepartment = "Write XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

Return False;

}

}

///

/// Update data, this time you want to ensure that Strfields is consistent with the two arrays of STRDATA

///

/// Does update success

Public Bool Update ()

{

IF (this.myds == null) .open ();

Try

{

DataRow [] editrow = myds.tables [0] .select (this.strfilter);

For (int J = 0; j

{

For (int i = 0; i

{

Editrow [J] [this.strfields [i]] = this.strdata [i];

}

THIS.SAVE ();

Return True;

}

Catch (Exception EE)

{

LOG log = new log ();

Log.struser = "system";

Log.StrDepartment = "Update XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

Return False;

}

}

///

/// delete data

///

/// Whether to delete success

Public bool delete ()

{

IF (this.myds == null) .open ();

Try

{

DataRow [] editrow = myds.tables [0] .select (this.strfilter);

For (int i = 0; i

{

Editrow [i] .delete ();

}

THIS.SAVE ();

Return True;

}

Catch (Exception EE)

{

LOG log = new log ();

Log.struser = "system";

Log.StrDepartment = "Delete XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

Return False;

}

}

///

/// According to a template, create a new XML file (provided that there must be a template file and determine the target file path)

///

/// Write to success

Public Bool Create ()

{

Try

{

XmLDocument Doc = New XmLDocument ();

XmlTextReader Reader = New XmlTextReader (this.strtemplatefile);

Doc.Load (Reader);

XMLELEMENT MEMBER;

XMLNode root = doc.documentelement;

For (int i = 0; i

{

MEMBER = Doc.createElement (Strfields [i] .tostring ());

Member.innertext = this.strdata [i] .tostring ();

Root.Appendchild (Member);

}

XmlTextWriter XMLWriter = New XmlTextWriter (this.strdatafile, NULL);

XMLWriter.Formatting = formatting.indented;

Doc.save (XMLWRITER);

XMLWriter.Close ();

Reader.Close ();

Return True;

}

Catch (Exception EE) {

LOG log = new log ();

Log.struser = "system";

Log.STRDEPARTMENT = "New XML Data";

Log.strfilename = "com.sem.tools.xmldatabase";

Log.STRDESCRIPTION = EE.MESSAGE;

Log.writelog ();

Return False;

}

}

///

/// Release resource

///

Public void clear ()

{

IF (this.myds! = null)

{

THIS.MYDS.DISPOSPOSE ();

}

}

}

}

Another LOG processing class is introduced into the class, and it is sent here for yoursTem.

USING SYSTEM.XML;

Using system.Web;

Namespace xmlbook.com.sem.tools

{

///

/// copyright: Copyright by SEM IT Department

/// version: 0.0.1

/// File: xmlbook.com.sem.tools.log.cs

/// Objective: Provide a way to write logs (that is, write errors into XML records)

/// Author: European Yuanning @ 2005-04-09

/// Mailbox: Outrace@soueast-motor.com

/// Modify:

///

Public Class log

{

#Region private member

Private httpcontext objcontext = httpContext.current;

///

// Log file path

///

Private string logfile = null;

///

/// operator

///

PRIVATE STRING STRUSER = NULL;

///

/// All version of the block

///

Private string strdepartment = null;

///

/ / Operating file name

///

Private string strfilename = null;

///

/// operation time

///

Private string strtime = null;

///

/// wrong description

///

PRIVATE STRING STRDESCRIPTION = NULL;

#ndregion

#Region Public property

///

/// operator

///

Public String Struser

{

Get {return this.struser;}

Set {this.struser = value;

}

///

/ / Operating file name

///

Public String StrfileName

{

Get {return this.strfilename;} set {this.strfilename = value;

}

///

/// All version of the block

///

Public String StrDepartment

{

Get {return this.strdepartment;

Set {this .strdepartment = value;

}

///

/// operation time

///

Public String Strtime

{

Get {return this.strtime;}

Set {this.strtime = value;

}

///

/// wrong description

///

Public String Strdescription

{

Get {return this.strdescription;

Set {this.strdescription = value;

}

#ndregion

Public log ()

{

//

// TODO: Provides a way to write logs (that is, write errors into XML records)

//

}

///

/// Write the content into the log file

///

Public void writelog ()

{

THIS.LOGFILE = this.objcontext.server.mappath ("./ log / log.config");

Try

{

XmLDocument Doc = New XmLDocument ();

Doc.Load (this.logfile);

Xmlelement newlog = doc.createElement ("log");

Xmlelement Newuser = Doc.createElement ("User");

NEWUSER.INNERTEXT = this.Struser;

Newlog.Appendchild (Newuser);

Xmlelement NewDepartment = Doc.createElement ("Department");

NewDepartment.innertext = this.StrDepartment;

Newlog.Appendchild (NewDepartment);

Xmlelement NewFileName = Doc.createElement ("FileName");

NewFilename.innertext = this.strfilename;

Newlog.Appendchild (NewFileName);

Xmlelement NewTime = Doc.createElement ("Time");

NEWTIME.INNNERTEXT = DATETIME.NOW.TOSTRING ();

Newlog.Appendchild (NewTime);

Xmlelement NewDescription = Doc.createElement ("Description");

NewDescription.INNNNERTEXT = this.StrDescription;

Newlog.Appendchild (NewDescription);

Doc.documentelement.Appendchild (newlog);

Doc.save (this.logfile);

}

Catch (Exception EX) {

HttpContext Objcontext = httpContext.current;

Objcontext.Response.write (ex.Message);

}

Finally

{

}

}

}

}

This way we can easily use an XML file as an example of using an example, I will give it to the next one.

转载请注明原文地址:https://www.9cbs.com/read-32773.html

New Post(0)