xml文件格式
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<messageList>
<message id="1">
<Name>熊猫</Name>
<QQ>123456</QQ>
<Email>panda@qq.com</Email>
<Content>asfdsafasdfasdfsad我是内容</Content>
<Time>2009-10-1</Time>
</message>
</messageList>
<?xml version="1.0" encoding="utf-8"?>
<messageList>
<message id="1">
<Name>熊猫</Name>
<QQ>123456</QQ>
<Email>panda@qq.com</Email>
<Content>asfdsafasdfasdfsad我是内容</Content>
<Time>2009-10-1</Time>
</message>
</messageList>
增加>>>>
view plaincopy to clipboardprint?
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载xml文件
XmlNode root = xmld.SelectSingleNode("messageList");//查找messageList节点
int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
XmlElement xml0 = xmld.CreateElement("message");
xml0.SetAttribute("id", i.ToString());
XmlElement xml1 = xmld.CreateElement("Name");//创建一个Name节点
xml1.InnerText = this.txtName.Value.Trim();
xml0.AppendChild(xml1);//添加
XmlElement xml2 = xmld.CreateElement("QQ");//创建QQ节点
xml2.InnerText = this.txtQQ.Value.Trim();
xml0.AppendChild(xml2);//添加
XmlElement xml3 = xmld.CreateElement("Email");//创建一个Email节点
xml3.InnerText = this.txtEmail.Value.Trim();
xml0.AppendChild(xml3);//添加
XmlElement xml4 = xmld.CreateElement("Content");//创建一个Content节点
xml4.InnerText = this.txtContent.Value;
xml0.AppendChild(xml4);//添加
XmlElement xml5 = xmld.CreateElement("Time");//创建一个Time节点
xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
xml0.AppendChild(xml5);//添加
root.AppendChild(xml0);
xmld.Save(Server.MapPath("App_Code/Message.xml"));
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载xml文件
XmlNode root = xmld.SelectSingleNode("messageList");//查找messageList节点
int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
XmlElement xml0 = xmld.CreateElement("message");
xml0.SetAttribute("id", i.ToString());
XmlElement xml1 = xmld.CreateElement("Name");//创建一个Name节点
xml1.InnerText = this.txtName.Value.Trim();
xml0.AppendChild(xml1);//添加
XmlElement xml2 = xmld.CreateElement("QQ");//创建QQ节点
xml2.InnerText = this.txtQQ.Value.Trim();
xml0.AppendChild(xml2);//添加
XmlElement xml3 = xmld.CreateElement("Email");//创建一个Email节点
xml3.InnerText = this.txtEmail.Value.Trim();
xml0.AppendChild(xml3);//添加
XmlElement xml4 = xmld.CreateElement("Content");//创建一个Content节点
xml4.InnerText = this.txtContent.Value;
xml0.AppendChild(xml4);//添加
XmlElement xml5 = xmld.CreateElement("Time");//创建一个Time节点
xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
xml0.AppendChild(xml5);//添加
root.AppendChild(xml0);
xmld.Save(Server.MapPath("App_Code/Message.xml"));
删除>>>>
view plaincopy to clipboardprint?
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode var in list)
{
XmlElement xmle = (XmlElement)var;
if (xmle.GetAttribute("id") == Id)//如果ID等于文本的值就删除
{
xmle.RemoveAll();
xmld.Save(Server.MapPath("App_Code/Message.xml"));//删除后记得一定要保存
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!!');", true);
}
}
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode var in list)
{
XmlElement xmle = (XmlElement)var;
if (xmle.GetAttribute("id") == Id)//如果ID等于文本的值就删除
{
xmle.RemoveAll();
xmld.Save(Server.MapPath("App_Code/Message.xml"));//删除后记得一定要保存
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!!');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!!');", true);
}
}
更改>>>>
view plaincopy to clipboardprint?
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode xmln in list)
{
XmlElement xmle = (XmlElement)xmln;//格式转换
if (Request["ID"].Equals(xmle.GetAttribute("id")))
{
xmle.ChildNodes[0].InnerText = this.txtName.Value;
xmle.ChildNodes[1].InnerText = this.txtQQ.Value;
xmle.ChildNodes[2].InnerText = this.txtEmail.Value;
xmle.ChildNodes[3].InnerText = this.txtContent.Value;
xmld.Save(Server.MapPath("App_Code/Message.xml"));
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!!');location.href='MessageList.aspx'",true);
}
}
XmlDocument xmld = new XmlDocument();
xmld.Load(Server.MapPath("App_Code/Message.xml"));
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
foreach (XmlNode xmln in list)
{
XmlElement xmle = (XmlElement)xmln;//格式转换
if (Request["ID"].Equals(xmle.GetAttribute("id")))
{
xmle.ChildNodes[0].InnerText = this.txtName.Value;
xmle.ChildNodes[1].InnerText = this.txtQQ.Value;
xmle.ChildNodes[2].InnerText = this.txtEmail.Value;
xmle.ChildNodes[3].InnerText = this.txtContent.Value;
xmld.Save(Server.MapPath("App_Code/Message.xml"));
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!!');location.href='MessageList.aspx'",true);
}
}
查询>>>>>
view plaincopy to clipboardprint?
//下面的代码开始是用页面编码的方式实现的
XmlDocument xmld = new XmlDocument();//构建xml文档
xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载
XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;//获取子节点
if (list!=null)
{
foreach (XmlNode node in list)
{
XmlElement xmle = (XmlElement)node;//转换
//这里到时候明天补上
//---------------------------------------------------------------------------------------------------
XmlNodeReader reader = null;
try
{
string s = "" ;
XmlDocument doc = new XmlDocument ( ) ;
// 装入指定的XML文档
doc.Load ( "C:\\Documents and Settings\\Administrator\\桌面\\1.xml" ) ;
// 设定XmlNodeReader对象来打开XML文件
reader = new XmlNodeReader ( doc ) ;
// 读取XML文件中的数据,并显示出来
while ( reader.Read ( ) )
{
//判断当前读取得节点类型
switch ( reader.NodeType )
{
case XmlNodeType.Element :
s = reader.Name ;
break ;
case XmlNodeType.Text :
if (s.Equals("name"))
myItem = Listview1.Items.Add ( reader.Value ) ;
else
myItem.SubItems.Add ( reader.Value ) ;
break ;
}
}
}
finally
{
//清除打开的数据流
if ( reader != null )
reader.Close ( ) ;
}
}
//---------------------------------------------------------------------------------------------------
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhoukang0916/archive/2009/10/17/4684210.aspx