读书人

在XML文件中怎么做似SQL中的SUM()方

发布时间: 2011-12-24 23:03:24 作者: rapoo

在XML文件中如何做似SQL中的SUM()方法
Xml文件如下:
<?xml version= "1.0 " encoding= "utf-8 " ?>
<Table_Payment>
<payment ID= "1 " YEAR= "2006 " MONTH= "12 " DAY= "27 " TypeID= "2 " CONTENT= "衣服 " MONEY= "500.0000 "/>
<payment ID= "2 " YEAR= "2006 " MONTH= "12 " DAY= "27 " TypeID= "2 " CONTENT= "衣服 " MONEY= "500.0000 "/>
</Table_Payment>
在我想[MONEY]行似于sql中的按年月Sum(),不能,我采取的方法是先xml文件入DataSet,然后使用DataTable的Compute方法行sum累,搞了一上午法通,求能者指教。
附上我的C#代:
int intYear=2007;
int intMonth=1;
DataSet ds = new DataSet();
ds.ReadXml(System.Windows.Forms.Application.StartupPath+ @ "\Payment.xml ");
object ot = ds.Tables[0].Compute( "Sum(Convert(Double,[Money])) ", "Year = ' ' "+intYear+ " ' ' and Month= ' ' "+intMonth+ " ' ' ");

[解决办法]
try..

int intYear = 2006;
int intMonth = 12;
DataSet ds = new DataSet();
ds.ReadXml(@ "../../test.xml ");
DataColumn dc = new DataColumn( "total ");
dc.DataType = System.Type.GetType( "System.Decimal ");
dc.Expression = "Convert(MONEY, 'System.Decimal ') ";
ds.Tables[0].Columns.Add(dc);
object ot = ds.Tables[0].Compute( "Sum(total) ", "YEAR = ' " + intYear + " ' and MONTH= ' " + intMonth + " ' ");
Console.WriteLine(ot.ToString());

读书人网 >C#

热点推荐