Message.xml
<?xml version="1.0" encoding="utf-8"?>
<body num="1">
<message>
<ID>1</ID>
<username>fasfd</username>
<content>asdfsadf</content>
<addtime>2010-08-09 17:58:05</addtime>
<isdel>0</isdel>
</message>
<body num="1">
<message>
<ID>1</ID>
<username>fasfd</username>
<content>asdfsadf</content>
<addtime>2010-08-09 17:58:05</addtime>
<isdel>0</isdel>
</message>
</body>
读取xml绑定repeater
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected
void
messagebind() {
DataTable dt = new DataTable( " dt " );
DataColumn dc = new DataColumn( " ID " );
dt.Columns.Add(dc);
dc = new DataColumn( " username " );
dt.Columns.Add(dc);
dc = new DataColumn( " content " );
dt.Columns.Add(dc);
dc = new DataColumn( " addtime " );
dt.Columns.Add(dc);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " Message.xml " ));
XmlNodeList xnl = xmlDoc.SelectNodes( " //body/message[isdel='0'] " ); // 条件
for ( int i = 0 ; i < xnl.Count; i ++ )
{
string id = xnl.Item(i).ChildNodes[ 0 ].InnerText;
string username = xnl.Item(i).ChildNodes[ 1 ].InnerText;
string content = xnl.Item(i).ChildNodes[ 2 ].InnerText;
string addtime = xnl.Item(i).ChildNodes[ 3 ].InnerText;
DataRow dr = dt.NewRow();
dr[ " ID " ] = id;
dr[ " username " ] = username;
dr[ " content " ] = content;
dr[ " addtime " ] = addtime;
dt.Rows.Add(dr);
}
ViewState[ " RecordCount " ] = dt.Rows.Count ; // 记录数
ViewState[ " PageSize " ] = 3 ;
ViewState[ " PageIndex " ] = Common.Request.GetInt( " p " , 1 );
if ( Convert.ToInt32( ViewState[ " PageIndex " ]) > (Convert.ToInt32(ViewState[ " RecordCount " ]) / Convert.ToInt32(ViewState[ " PageSize " ]) + 1 ))
{
ViewState[ " PageIndex " ] = 1 ;
}
this .repeater1.DataSource = GetPagedTable(dt, Convert.ToInt32(ViewState[ " PageIndex " ]), Convert.ToInt32(ViewState[ " PageSize " ]));
this .repeater1.DataBind();
this .CustomPager1.UrlManager = new DefaultUrlManager(Convert.ToInt32(ViewState[ " RecordCount " ]), Convert.ToInt32(ViewState[ " PageSize " ]), " p " );
}
DataTable dt = new DataTable( " dt " );
DataColumn dc = new DataColumn( " ID " );
dt.Columns.Add(dc);
dc = new DataColumn( " username " );
dt.Columns.Add(dc);
dc = new DataColumn( " content " );
dt.Columns.Add(dc);
dc = new DataColumn( " addtime " );
dt.Columns.Add(dc);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " Message.xml " ));
XmlNodeList xnl = xmlDoc.SelectNodes( " //body/message[isdel='0'] " ); // 条件
for ( int i = 0 ; i < xnl.Count; i ++ )
{
string id = xnl.Item(i).ChildNodes[ 0 ].InnerText;
string username = xnl.Item(i).ChildNodes[ 1 ].InnerText;
string content = xnl.Item(i).ChildNodes[ 2 ].InnerText;
string addtime = xnl.Item(i).ChildNodes[ 3 ].InnerText;
DataRow dr = dt.NewRow();
dr[ " ID " ] = id;
dr[ " username " ] = username;
dr[ " content " ] = content;
dr[ " addtime " ] = addtime;
dt.Rows.Add(dr);
}
ViewState[ " RecordCount " ] = dt.Rows.Count ; // 记录数
ViewState[ " PageSize " ] = 3 ;
ViewState[ " PageIndex " ] = Common.Request.GetInt( " p " , 1 );
if ( Convert.ToInt32( ViewState[ " PageIndex " ]) > (Convert.ToInt32(ViewState[ " RecordCount " ]) / Convert.ToInt32(ViewState[ " PageSize " ]) + 1 ))
{
ViewState[ " PageIndex " ] = 1 ;
}
this .repeater1.DataSource = GetPagedTable(dt, Convert.ToInt32(ViewState[ " PageIndex " ]), Convert.ToInt32(ViewState[ " PageSize " ]));
this .repeater1.DataBind();
this .CustomPager1.UrlManager = new DefaultUrlManager(Convert.ToInt32(ViewState[ " RecordCount " ]), Convert.ToInt32(ViewState[ " PageSize " ]), " p " );
}
添加:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
代码
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string
username
=
this
.txt_username.Text.Trim();
string
content
=
this
.txt_content.Text.Trim();
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath(
"
Message.xml
"
));
//
加载文件
XmlNode root
=
xmlDoc.SelectSingleNode(
"
body
"
);
//
查找<dbGuest>
XmlElement xml_message
=
xmlDoc.CreateElement(
"
message
"
);
//
创建一个<community>节点
XmlElement xml_ID
=
xmlDoc.CreateElement(
"
ID
"
);
//
创建一个<community>节点
XmlElement xml_username
=
xmlDoc.CreateElement(
"
username
"
);
//
创建一个<community>节点
XmlElement xml_content
=
xmlDoc.CreateElement(
"
content
"
);
//
创建一个<community>节点
XmlElement xml_addtime
=
xmlDoc.CreateElement(
"
addtime
"
);
//
创建一个<community>节点
XmlElement xml_isdel
=
xmlDoc.CreateElement(
"
isdel
"
);
//
创建一个<isdel>节点
string
num
=
root.Attributes[
"
num
"
].Value;
//
body 的num属性表示总数
xml_ID.InnerText
=
Convert.ToString(Convert.ToInt32(num)
+
1
);
root.Attributes[
"
num
"
].Value
=
xml_ID.InnerText;
xml_username.InnerText
=
username;
xml_content.InnerText
=
content;
xml_addtime.InnerText
=
DateTime.Now.ToString();
xml_isdel.InnerText
=
"
0
"
;
xml_message.AppendChild(xml_ID);
xml_message.AppendChild(xml_username);
xml_message.AppendChild(xml_content);
xml_message.AppendChild(xml_addtime);
xml_message.AppendChild(xml_isdel);
root.PrependChild(xml_message);
xmlDoc.Save(Server.MapPath(
"
Message.xml
"
));
//
保存文件
this
.txt_content.Text
=
string
.Empty;
this
.txt_username.Text
=
string
.Empty;
//
Common.JsUtil.ShowMsg("提交成功!", "Default.aspx");
messagebind();
}
删除:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
代码
protected
void
repeater1_ItemCommand(
object
source, RepeaterCommandEventArgs e)
{
if
(e.CommandName
==
"
del
"
)
{
string
id
=
e.CommandArgument.ToString();
///
/Response.Write(id);
//
this.Label1.Text = id;
XmlDocument xmlDoc
=
new
XmlDocument();
xmlDoc.Load(Server.MapPath(
"
Message.xml
"
));
XmlNodeList xnl
=
xmlDoc.SelectNodes(
string
.Format(
"
//body/message[ID='{0}']
"
,id));
//
条件
xnl.Item(
0
).ChildNodes[
4
].InnerText
=
"
1
"
;
xmlDoc.Save(Server.MapPath(
"
Message.xml
"
));
//
保存文件
messagebind();
}
}
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
代码
public
DataTable GetPagedTable(DataTable dt,
int
PageIndex,
int
PageSize)
{
if
(PageIndex
==
0
)
return
dt;
DataTable newdt
=
dt.Copy();
newdt.Clear();
int
rowbegin
=
(PageIndex
-
1
)
*
PageSize;
int
rowend
=
PageIndex
*
PageSize;
if
(rowbegin
>=
dt.Rows.Count)
return
newdt;
if
(rowend
>
dt.Rows.Count)
rowend
=
dt.Rows.Count;
for
(
int
i
=
rowbegin; i
<=
rowend
-
1
; i
++
)
{
DataRow newdr
=
newdt.NewRow();
DataRow dr
=
dt.Rows[i];
foreach
(DataColumn column
in
dt.Columns)
{
newdr[column.ColumnName]
=
dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return
newdt;
}
写的不好,请高手多多指教