A class that uses dataset to operate XML

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. xml;

//************************************** *
// Prepared by: # go to dinner tomorrow
// Qicq: 305725744
//. Net group: 6370988
//Http://blog.csdn.net/kgdiwss
//************************************** *

Namespace dataset2xml. Component
{
/// <Summary>
/// Operatexmlbydataset abstract description.
/// </Summary>
Public class operatexmlbydataset
{
Public operatexmlbydataset ()
{
//
// Todo: add the constructor logic here
//
}

# Region getdatasetbyxml
/// <Summary>
/// Read XML and return dataset directly
/// </Summary>
/// <Param name = "strxmlpath"> relative path of the XML file </param>
/// <Returns> </returns>
Public static dataset getdatasetbyxml (string strxmlpath)
{
Try
{
Dataset DS = new dataset ();
DS. readxml (getxmlfullpath (strxmlpath ));
If (Ds. Tables. Count> 0)
{
Return Ds;
}
Return NULL;
}
Catch (exception)
{
Return NULL;
}
}
# Endregion

# Region getdataviewbyxml
/// <Summary>
/// Read the XML and return a sorted or filtered dataview.
/// </Summary>
/// <Param name = "strxmlpath"> </param>
/// <Param name = "strwhere"> filter conditions, such as: "name = 'kgdiwss'" </param>
/// <Param name = "strsort"> sorting condition, for example, "id DESC" </param>
/// <Returns> </returns>
Public static dataview getdataviewbyxml (string strxmlpath, string strwhere, string strsort)
{
Try
{
Dataset DS = new dataset ();
DS. readxml (getxmlfullpath (strxmlpath ));
Dataview DV = new dataview (Ds. Tables [0]);
If (strsort! = NULL)
{
DV. Sort = strsort;
}
If (strwhere! = NULL)
{
DV. rowfilter = strwhere;
}
Return DV;
}
Catch (exception)
{
Return NULL;
}
}
# Endregion

# Region writexmlbydataset
/// <Summary>
/// Insert a row of data into the XML file
/// </Summary>
/// <Param name = "strxmlpath"> relative path of the XML file </param>
/// <Param name = "columns"> array of column names to insert rows, for example, string [] Columns = {"name", "ismarried"}; </param>
/// <Param name = "columnvalue"> insert an array of values in each column of the row, for example, string [] columnvalue = {"tomorrow", "false "}; </param>
/// <Returns> success returns true; otherwise, false is returned. </returns>
Public static bool writexmlbydataset (string strxmlpath, string [] columns, string [] columnvalue)
{
Try
{
// Obtain the. XSD path based on the input XML Path. The two files are stored in the same directory.
String strxsdpath = strxmlpath. substring (0, strxmlpath. indexof (".") + ". XSD ";

Dataset DS = new dataset ();
// Read the XML schema, which is related to the column data type
DS. readxmlschema (getxmlfullpath (strxsdpath ));
DS. readxml (getxmlfullpath (strxmlpath ));
Datatable dt = Ds. Tables [0];
// Create a new row based on the original table
Datarow newrow = DT. newrow ();

// Cyclically assign values to each column in a row
For (INT I = 0; I <columns. length; I ++)
{
Newrow [columns [I] = columnvalue [I];
}
DT. Rows. Add (newrow );
DT. acceptchanges ();
DS. acceptchanges ();

DS. writexml (getxmlfullpath (strxmlpath ));
Return true;
}
Catch (exception)
{
Return false;
}
}
# Endregion

# Region updatexmlrow
/// <Summary>
/// A qualified XML record for a row
/// </Summary>
/// <Param name = "strxmlpath"> XML file path </param>
/// <Param name = "columns"> array of column names </param>
/// <Param name = "columnvalue"> array of column values </param>
/// <Param name = "strwherecolumnname"> condition column name </param>
/// <Param name = "strwherecolumnvalue"> condition column value </param>
/// <Returns> </returns>
Public static bool updatexmlrow (string strxmlpath, string [] columns, string [] columnvalue, string strwherecolumnname, string strwherecolumnvalue)
{
Try
{
String strxsdpath = strxmlpath. substring (0, strxmlpath. indexof (".") + ". XSD ";

Dataset DS = new dataset ();
// Read the XML schema, which is related to the column data type
DS. readxmlschema (getxmlfullpath (strxsdpath ));
DS. readxml (getxmlfullpath (strxmlpath ));

// Determine the number of rows first
If (Ds. Tables [0]. Rows. Count> 0)
{
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
// If the current record is a record that meets the where Condition
If (Ds. Tables [0]. Rows [I] [strwherecolumnname]. tostring (). Trim (). Equals (strwherecolumnvalue ))
{
// Cyclically add new values to columns of the row
For (Int J = 0; j <columns. length; j ++)
{
DS. Tables [0]. Rows [I] [columns [J] = columnvalue [J];
}
// Update Dataset
DS. acceptchanges ();
// Re-write the XML file
DS. writexml (getxmlfullpath (strxmlpath ));
Return true;
}
}

}
Return false;
}
Catch (exception)
{
Return false;
}
}
# Endregion

# Region deletexmlrowbyindex
/// <Summary>
/// Delete the ideleterow row in dataset, and then rewrite the XML to delete the specified row.
/// </Summary>
/// <Param name = "strxmlpath"> </param>
/// <Param name = "ideleterow"> index value of the row to be deleted in dataset </param>
Public static bool deletexmlrowbyindex (string strxmlpath, int ideleterow)
{
Try
{
Dataset DS = new dataset ();
DS. readxml (getxmlfullpath (strxmlpath ));
If (Ds. Tables [0]. Rows. Count> 0)
{
// Delete the row of the symbol Condition
DS. Tables [0]. Rows [ideleterow]. Delete ();
}
DS. writexml (getxmlfullpath (strxmlpath ));
Return true;
}
Catch (exception)
{
Return false;
}
}
# Endregion

# Region deletexmlrows
/// <Summary>
/// Delete the row whose value is columnvalue in the strcolumn
/// </Summary>
/// <Param name = "strxmlpath"> XML relative path </param>
/// <Param name = "strcolumn"> column name </param>
/// <Param name = "columnvalue"> all rows whose values are columnvalue in the strcolumn are deleted </param>
/// <Returns> </returns>
Public static bool deletexmlrows (string strxmlpath, string strcolumn, string [] columnvalue)
{
Try
{
Dataset DS = new dataset ();
DS. readxml (getxmlfullpath (strxmlpath ));

// Determine the number of rows first
If (Ds. Tables [0]. Rows. Count> 0)
{
// Determine whether there are multiple rows or more Deleted Values, And put multiple for loops in it
If (columnvalue. length> DS. Tables [0]. Rows. Count)
{
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
For (Int J = 0; j <columnvalue. length; j ++)
{
If (Ds. Tables [0]. Rows [I] [strcolumn]. tostring (). Trim (). Equals (columnvalue [J])
{
DS. Tables [0]. Rows [I]. Delete ();
}
}
}
}
Else
{
For (Int J = 0; j <columnvalue. length; j ++)
{
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
If (Ds. Tables [0]. Rows [I] [strcolumn]. tostring (). Trim (). Equals (columnvalue [J])
{
DS. Tables [0]. Rows [I]. Delete ();
}
}
}
}
DS. writexml (getxmlfullpath (strxmlpath ));
}
Return true;
}
Catch (exception)
{
Return false;
}
}
# Endregion

# Region deletexmlallrows
/// <Summary>
/// Delete all rows
/// </Summary>
/// <Param name = "strxmlpath"> XML Path </param>
/// <Returns> </returns>
Public static bool deletexmlallrows (string strxmlpath)
{
Try
{
Dataset DS = new dataset ();
DS. readxml (getxmlfullpath (strxmlpath ));
// If the number of records is greater than 0
If (Ds. Tables [0]. Rows. Count> 0)
{
// Remove all records
DS. Tables [0]. Rows. Clear ();
}
// Re-write. At this time, only the root node is left in the XML file.
DS. writexml (getxmlfullpath (strxmlpath ));
Return true;
}
Catch (exception)
{
Return false;
}
}
# Endregion

# Region getxmlfullpath
/// <Summary>
/// Return the complete path
/// </Summary>
/// <Param name = "strpath"> XML Path </param>
/// <Returns> </returns>
Public static string getxmlfullpath (string strpath)
{
If (strpath. indexof (":")> 0)
{
Return strpath;
}
Else
{
Return System. Web. httpcontext. Current. server. mappath (strpath );
}
}
# Endregion
}
}

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.