Xml充当数据库的应用
现在Xml的应用几乎无处不在了,偶而当数据量固定,用作数据库也是不错的。我所知道的,比如软件的皮肤,邮箱的设置,还有一些网站常量的设置。。。有兴趣或需要时,是个不错的选择。
最近做了帮武汉大学自考招办做了个小型的报名系统,开始时选用xml作为数据库的,但最终还是放弃了,其中的利弊自己体会吧。。
<?xml version="1.0" standalone="yes"?> |
<dsColumnSetting> |
<xs:schema id="dsColumnSetting" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> |
<xs:element name="dsColumnSetting" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> |
<xs:complexType> |
<xs:choice minOccurs="0" maxOccurs="unbounded"> |
<xs:element name="tbColumnSetting"> |
<xs:complexType> |
<xs:sequence> |
<xs:element name="colId" type="xs:string" minOccurs="0" /> |
<xs:element name="colName" type="xs:string" minOccurs="0" /> |
<xs:element name="colWidth" type="xs:string" minOccurs="0" /> |
<xs:element name="colHeader" type="xs:string" minOccurs="0" /> |
<xs:element name="colDataKey" type="xs:string" minOccurs="0" /> |
<xs:element name="colViewName" type="xs:string" minOccurs="0" /> |
<xs:element name="colSort" type="xs:string" minOccurs="0" /> |
</xs:sequence> |
</xs:complexType> |
</xs:element> |
</xs:choice> |
</xs:complexType> |
</xs:element> |
</xs:schema> |
<tbColumnSetting> |
<colId>1</colId> |
<colName>客户查询</colName> |
<colWidth>210,85,80,58,90,90,300,150,150,100</colWidth> |
<colHeader>客户名称, 客户类型, 所在地区,结算类型, 信用额度, 预警金额,授权样本, 联系电话, 客户邮箱, 客户邮编</colHeader> |
<colDataKey>fBCName, fBCTName, fBRName,fBCPaymentType, fBCCRate, fBCCAlarmRate,fSName, fBCTels,fBCEmaile, fBCPostCode</colDataKey> |
<colViewName>vCustomerSearch</colViewName> |
<colSort>fBCName</colSort> |
</tbColumnSetting></dsColumnSetting> |
2.操作xml数据库
//验证歌曲重复,并添加到播放列表 |
DataSet _dsSong = new DataSet(); |
_dsSong.ReadXml(Application.StartupPath + "\\musicList.xml", XmlReadMode.ReadSchema); |
DataView _dv = new DataView(_dsSong.Tables[0]); |
_dv.RowFilter = string.Format("mUrl='{0}'",clsTool.SqlInsertEncode(ofd.FileName));//筛选 |
//添加数据 |
DataRow _dr = _dsSong.Tables[0].NewRow(); |
_dsSong.Tables[0].Rows.Add(_dr); |
_dsSong.WriteXml(Application.StartupPath+ "\\musicList.xml", XmlWriteMode.WriteSchema); |
//更新 |
foreach (DataRow dr in _dsSong.Tables[0].Rows) |
{ |
if (dr["col_Id"].ToString()==this.lb_HideId.Text) |
dr[clsTools.ColExchange(lb_new.Text.Substring(1))]=this.tb_new.Text.Trim(); |
} |
//删除数据 |
DataSet _dsSong = new DataSet(); |
_dsSong.ReadXml(Application.StartupPath + "\\reg.xml", XmlReadMode.ReadSchema); |
DataView _dv = new DataView(_dsSong.Tables[0]); |
_dv.RowFilter = string.Format("col_Id<>'{0}'",strId); |
_dv.ToTable().WriteXml(Application.StartupPath + "\\reg.xml", XmlWriteMode.WriteSchema); |